Akshay Rawal wrote:
final String s1 = "str";
final String s2 = "ing";
String concat = s1 + s2 ; // it reurns new object and place in string constant pool..just a guess..
System.out.println(concat == "string"); //true.....can anyone please help me the reason why both are giving unexpected answer
Interestingly, no. Or perhaps ... almost.
s1, s2, "str", "ing", and "string" are all compile time constants. The compiler knows what every one of these values are at compile time. And hence, it is like the code you had were ...
So, yes, you can can argue that an object is created and placed in the string pool... but the object is not created from the concatenation operation.
Also note that the "str" and "ing" objects are *not* created at all !!
Henry