String is Immutable (Fact)
Lets say I have two objects of string given below:
String str1 = new String("dave");
String str2 = new String("dave");
While compiling, Interning of string objects happens. And according to Wikipedia, It says "string interning is a method of storing only one copy of each distinct string value, which must be immutable. All compile-time constant strings in Java are automatically interned using this method."
If that's a case, then what would happen to my two references str1 and str2 ? To which object they will point out once compiled ?
I hope its valid question, otherwise correct me, I'm confused little bit.
str1andstr2are not compile time constants, since they are both instantiated withnew- khelwood"dave"may be interned, but the objects held bystr1andstr2are different objects each created bynewat runtime. They do indeed exist individually. - khelwoodstr1 != str2 && str2 != "dave", && str2 != "dave"- Kevin AndersonSystem.out.println(str1 == str2);answer your question?new String("dave")is NOT the same as"dave". - Bernhard Barker