mercredi 11 mars 2015

relationship between pool and heap [duplicate]


See the code.



String xx=new String("hello world");
String yy="hello world";

System.out.println("xx: " + xx);
System.out.println("yy: " + yy);
System.out.println("xx==yy:"+(xx==yy)); // false

//modify the value of yy using reflection
Field yyfield=String.class.getDeclaredField("value");
yyfield.setAccessible(true);
char[] value=(char[])yyfield.get(yy); // change the value of yy
value[5]='_';

System.out.println("xx: " + xx); // xx's value also changed.why?
System.out.println("yy: " + yy);
System.out.println("xx==yy:"+(xx==yy)); //false


I've seen some posts about Literal Pool, know xx and yy point to different place. But why I change the value of yy, xx also changed. Is there any operation in reflection or some other aspects I don't know? Thanks in advance.






Aucun commentaire:

Enregistrer un commentaire