Skip to content

Commit 4d01e00

Browse files
committed
✨ Introducing new features. String test
1 parent fadec01 commit 4d01e00

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

src/main/java/com/crossoverjie/basic/StringTest.java

+18-2
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,29 @@
1111
*/
1212
public class StringTest {
1313

14-
public static void main(String[] args) throws NoSuchFieldException {
14+
public static void main(String[] args) throws NoSuchFieldException, IllegalAccessException {
1515
String a = "123";
16+
//这里的 a 和 b 都是同一个对象,指向同一个字符串常量池对象。
17+
String b = "123" ;
18+
String c = new String("123") ;
19+
20+
System.out.println("a=b:" + (a == b));
21+
System.out.println("a=c:" + (a == c));
22+
1623
System.out.println("a=" + a);
1724

1825
a = "456";
1926
System.out.println("a=" + a);
2027

21-
Field value = a.getClass().getField("value");
28+
29+
//用反射的方式改变字符串的值
30+
Field value = a.getClass().getDeclaredField("value");
31+
//改变 value 的访问属性
32+
value.setAccessible(true) ;
33+
34+
char[] values = (char[]) value.get(a);
35+
values[0] = '9' ;
36+
37+
System.out.println(a);
2238
}
2339
}

0 commit comments

Comments
 (0)