I came across a question asking the output of the below:
String s1 = "String 1";
String s2 = "String 2";
String s3 = s1 + s2;
String s4 = "String 1" + "String 2";
System.out.println(s3==s4);
Output - false
Now, since the strings are not created using new operator, so the objects are created in the string pool, so as per my understanding s1 + s2
and "String 1" + "String 2"
should be equal, and s3==s4
should be true
.
But it is not happening in real. Can any one please explain this?