File tree 1 file changed +18
-2
lines changed
src/main/java/com/crossoverjie/basic
1 file changed +18
-2
lines changed Original file line number Diff line number Diff line change 11
11
*/
12
12
public class StringTest {
13
13
14
- public static void main (String [] args ) throws NoSuchFieldException {
14
+ public static void main (String [] args ) throws NoSuchFieldException , IllegalAccessException {
15
15
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
+
16
23
System .out .println ("a=" + a );
17
24
18
25
a = "456" ;
19
26
System .out .println ("a=" + a );
20
27
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 );
22
38
}
23
39
}
You can’t perform that action at this time.
0 commit comments