File tree Expand file tree Collapse file tree 1 file changed +18
-2
lines changed
src/main/java/com/crossoverjie/basic Expand file tree Collapse file tree 1 file changed +18
-2
lines changed Original file line number Diff line number Diff line change 1111 */
1212public 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}
You can’t perform that action at this time.
0 commit comments