Given the following code:
String str1 = new String("Hello");
String str2 = str1;
String str3 = new String(str1);
String str4 = str3;
str4 += " World ";
if (str3==str4)
System.out.println(“one”);
if (str3.equals(str4))
System.out.println(“two”);
if (str1==str2)
System.out.println(“three”);
if (str3.equals(str2))
System.out.println(“four”);
The output is : Three and Four
I don't get it.. we just did str3 == str4 . how can they NOT refer to the same object..? str3 == str4 seem to be false and I dont get why. In addition, the str3.equals(str4) also return false but I guess that has something to do with the first thing I dont get.
Would love to get an explanation.