Java?е?String???????????? String??????
???????????? ???????[ 2015/10/13 13:33:08 ] ???????????????????
??????????????????
????????????? ??Java?У? String???????????????????????????????? ????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????
????????????????????
????????Java?????? ????String???????????????????????????????
????String s = "ABCabc";
????System.out.println("s = " + s);
????s = "123456";
????System.out.println("s = " + s);
?????????????
????s = ABCabc
????s = 123456
??????????????String????s???????s????“ABCabc”?? ???????s????“123456”?? ????????????????s????????????????????String?????????????? ??????????????????? s??????String?????????????????????????????????????????????????????????????????????????????????4????????????????????????????????????????????????????
??????????s???????????????????????????????s=“123456”; ????????й???????????????μ????“123456”?? ??????s??????????????????????????“ABCabc”????????д????????и????????????????
????Java??C++????????????? ??Java?в?????????????????????е?????????????????????????????ò????????????????????????????????????????????????????????????????C++?д?????????????????????????????????????????????????????Java?е????ú?C++?е??????????????????????????????????????е???????????Java?У??????????????????????Java?е????ò?????C++?е???????????м??????
???????String????????????
????????String?????????????????String???ж?????Щ????????? ??JDK1.6?У?String???????????????????
????public final class String
????implements java.io.Serializable?? Comparable<String>?? CharSequence
????{
????/** The value is used for character storage. */
????private final char value[];
????/** The offset is the first index of the storage that is used. */
????private final int offset;
????/** The count is the number of characters in the String. */
????private final int count;
????/** Cache the hash code for the string */
????private int hash; // Default to 0
??????JDK1.7?У?String???????Щ??????????????substring??????????????????????????????JDK1.7??String????????????????????????
????public final class String
????implements java.io.Serializable?? Comparable<String>?? CharSequence {
????/** The value is used for character storage. */
????private final char value[];
????/** Cache the hash code for the string */
????private int hash; // Default to 0
?????????????????????? ??Java??String????????????????????JDK6?У? value??String????????飬offset??String?????value?????е????λ???count??String?????????????????JDK7?У???????value?????????value?е????????????????String?????????????????????????? ????????????hash????????????String????????????棬???????????????????????????Java?У???????????????ο???????????java?????????????? ????value?????????????????????????????????????????String s = “ABCabc”; ?????????????????沼??????????????
????value??offset??count??????????????private????????????setValue?? setOffset??setCount????????????????Щ?????????String???????????String?????????????????????? ??????String???????????????????????????value??offset??count??????????????final??? ??????String????????????????????????? ???????????????????String??????????????
?????????String?У??????????Щ?????????????????????????????Щ????????substring?? replace?? replaceAll?? toLowerCase??????????′???
????String a = "ABCabc";
????System.out.println("a = " + a);
????a = a.replace('A'?? 'a');
????System.out.println("a = " + a);
?????????????
????a = ABCabc
????a = aBCabc
???????a????????????????????????????????????? a??????????? ????????????????????????a.replace(‘A’?? ‘a’)??? ?????????????????μ?String??????????????????????????????a??String??replace???????????????????
?????????????????????????????????????????′????μ?String???????????????μ????????????????????????????????replace?? substring??toLowerCase???????????????????????????????????????ò????????????
????String ss = "123456";
????System.out.println("ss = " + ss);
????ss.replace('1'?? '0');
????System.out.println("ss = " + ss);
????????????
????ss = 123456
????ss = 123456
????String?????????????
????????????String??????????private final ??????????????????????????????У? value?????????????????????????????????????????value??final???ε??????final?????????????????????????????value?????????? ???罫?????е????λ????????????????“_”?? ?????????????д??????????в?????????????????????????????????value????????????????????????????顣
???????????????????????г????? ?????÷??? ????????String?????е?value????? ??????????????value???????????????????????????
????public static void testReflection() throws Exception {
????//?????????"Hello World"?? ??????????s
????String s = "Hello World";
????System.out.println("s = " + s); //Hello World
????//???String???е?value???
????Field valueFieldOfString = String.class.getDeclaredField("value");
????//???value???????????
????valueFieldOfString.setAccessible(true);
????//???s???????value??????
????char[] value = (char[]) valueFieldOfString.get(s);
????//???value????????????е??5?????
????value[5] = '_';
????System.out.println("s = " + s); //Hello_World
????}
?????????????
????s = Hello World
????s = Hello_World
??????????????У?s????????????String???????????????????String????????仯?? ???????????????????????ν??“?????”???????????????????????????????????????????????????????????????????????????????????????????????????????????????????????Car??????????????Wheel??????????Wheel????????????private final ??????????Wheel????????????????? ????????????Car???????
??????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11