String s1="Hello";
s1=s1.concat("World");
String s2="HelloWorld";
System.out.println(s1);
System.out.println(s2);
System.out.println(s2==s1); //false
As after concatenating, the "HelloWorld" string is created in the string constant pool and we are making another string with the same word "HelloWorld" then it is already present in the string constant pool hence it returns the existed reference. So, why my code is giving false in the output?
String s1="Hello";
String s2="HelloWorld";
s1=s1.concat("World");
System.out.println(s1);
System.out.println(s2);
System.out.println(s2==s1);//false
String s1="Hello";
s1=s1+"World";
String s2="HelloWorld";
System.out.println(s1);
System.out.println(s2);
System.out.println(s2==s1);//false
why false?? why they are pointing to different ref. As the word is already present in the string constant pool. then if we forming a new string object with the same value then it should point to the already present object.