java - String assignments and output -
given 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 : 3 , four
i don't it.. did str3 == str4 . how can not refer same object..? str3 == str4 seem false , dont why. in addition, str3.equals(str4) return false guess has first thing dont get.
would love explanation.
because string
immutable, +=
operator creates new instance , assignes str4
. therefore str4
not equal str3
.
Comments
Post a Comment