Changes in the String and constant pool values.
Public static void main (String [] args) {Integer i1 = new Integer (1); Integer i2 = new Integer (1); // i1, i2 is located in different memory spaces in the heap System. out. println ("i1 = i2:" + (i1 = i2); // outputs false String s1 = "china"; String s2 = "china "; string ss1 = new String ("china"); String ss2 = new String ("china"); System. out. println ("s1 = s2:" + (s1 = s2); System. out. println ("ss1 = ss2" + (ss1 = ss2); System. out. println ("s1 = ss2" + (s1 = ss2); // use the Java reflection mechanism to change the string value Field f = null; Field f1 = null; try {f = s1.getClass (). getDeclaredField ("value"); f1 = ss1.getClass (). getDeclaredField ("value");} catch (NoSuchFieldException e) {e. printStackTrace ();} f. setAccessible (true); f1.setAccessible (true); try {f. set (s1, new char [] {'C', 'h', 'I', 'n', 'B'}); f1.set (ss1, new char [] {'C', 'h', 'I', 'n', 'B'});} catch (Exception e) {e. printStackTrace ();} System. out. println ("s1 value after change:" + s1); System. out. println ("s2 value:" + s2); System. out. println ("the value of ss1 is:" + ss1); System. out. println ("s1 = s2:" + (s1 = s2); System. out. println ("ss1 = ss2" + (ss1 = ss2); System. out. println ("s1 = ss2" + (s1 = ss2); System. out. println ("================ split line ============================"); System. out. println ("value of the changed ss1:" + ss1); System. out. println ("the value of ss2 is:" + ss2 );}
Printed result:
I1 = i2: false
S1 = s2: true
Ss1 = ss2false
S1 = ss2false
Value of s1 after change: chinb
The value of s2 is chinb.
The value of ss1 is chinb.
S1 = s2: true
Ss1 = ss2false
S1 = ss2false
==============================
Value of the changed ss1: chinb
The value of ss2 is china.