The compiler doesn't know that the value of s is "abcd", so the new Strings are just that - newly created Strings. They aren't stored in the String pool.
If you would make s final then the compiler would see that the value of s will always be "abcd", and therefore can use the String literal "abcde" for both s1 and s2 and the result will be true.
This should also give me false right and only s.equals(s1) should give true?
I want to know both s1 and s2 in previously mentioned program contain same string then why two different references, both should refer to same reference and s1==s2 should give me true...
Please correct me....where I am going wrong...
The String s is not a compile-time constant, because the compiler cannot tell whether it has changed between line 5 and line 6. It might appear obvious to us reading it, but it would be very awkward to program the compiler to recognise the absence of changes. So it cannot put s1 and s2 into the String pool. Two possibilities:
Label s final; it then becomes a compile-time constant.
Use the intern method: (s + "e").intern() which intructs the JVM to attempt to insert that String into the String pool.
My Question is..
1. So it means executing line s1=s+"e"; puts the value "abcde" into s1 and that s1 value will not go in String pool?
2. How much String object is made after executing above line?
3. As per said, that s is not compile time constant, that is fine but value abcde hold by s1 after execution of the above line, that should go in String pool because if now value of s will change so what does it matters?