String s1 = "abc";
String s2 = "def";
String s3 = "abcdef";
String s4 = s1 + s2;
String s5 = "abc" + "def";
System.out.println(s3==s4); //gives false
System.out.println(s3==s5); //gives true
Can anyone explain why s3==s4 is giving false and s3==s is giving true? unable to understand the reason and internal working of what is actually happening here?
When I do concatenation of string like in s5, the logic that "abcdef" value is already there in string pool because of s3, for s5==s3 we will get true as s5 will also be pointed to the same s3 object present in stringpool in the heap.
Going the same logic, i.e. value already there is stringpool so new ref. variable will also point to same object, I thought s3==s4 would give true as s3 is already there is string pool, so when s4 is created it will point to same "abcdef" i.e. the s3 value in string pool.....but s3==s4 is giving false.....can someone please explain what am I missing here if s3==s5 is working and giving true...why isn't s3==s4 working and giving true as well?