You just changed the String Mario constant pool of String constants to Luigi , referenced by several String s, so every Mario link literal is now Luigi .
Field stringValue = String.class.getDeclaredField("value");
You have selected the char[] named value field from the String class
stringValue.setAccessible(true);
Make available.
stringValue.set(original, "Luigi".toCharArray());
You changed the original String field to Luigi . But the original Mario literal is String and the literal belongs to the String pool, and they are all interned. This means that all literals that have the same content refer to the same memory address.
String a = "Mario";//Created in String pool String b = "Mario";//Refers to the same Mario of String pool a == b//TRUE //You changed 'a' to Luigi and 'b' don't know that //'a' has been internally changed and //'b' still refers to the same address.
You basically changed the Mario pool from String , which is reflected in all the links. If you create a String Object (i.e. new String("Mario") ) instead of a literal, you will not encounter this behavior because you will have two different Mario .
coder-croc Sep 17 '15 at 6:50 2015-09-17 06:50
source share