I have been asked this question in an interview, I said both would return false. but when I crossed checked it one was true other was false
can anyone explain it?
String s1 = "H"; String s2 = "e"; String s3 = s1+s2; String s4 = "He"; System.out.println(s3 == s4);//false String s5="h"; String s6="h"; System.out.println(s5 == s6);//true
String s3 = s1+s2;
That string realized at run time and created a reference of that in heap.
when we concat strings with (+) operator JVM returns new
Where as in second case
String s5="h"; String s6="h";
s5 and s6 both are resolved at compile time and pointing to same reference((Constant pool)).