Say you have three strings,
String s1 = \"string one\";
String s2 = new String(\"string one\");
String s3 = \"string one\";
I know it is tr
above is HOW.
and You should know why.
A) string varaible declared in compile time ref to constant in constant pool
B) string varaible result by method ref to Object in heap space .
it all because JVM specfication and it's memory design.
A) is because strings are immutable。when java compile class and jvm load class , jvm found one String variable declared by your in coding time。 cause it is constant, jvm put the constant to "Runtime Constant Pool" memory area. and, constant is unique.
B) is verysimple. because jvm runtime variable use heap space.