Santosh Kumar Nayak wrote:Why does the str10 result as false ? Line no : 11
Because on line no 7 here this is not a constant expression so String concatenation is performed where
JLS 9 15.18.1. String Concatenation Operator + wrote:The result of string concatenation is a reference to a String object that is the concatenation of the two operand strings. The characters of the left-hand operand precede the characters of the right-hand operand in the newly created string.
The String object is newly created (§12.5) unless the expression is a constant expression (§15.28).
ergo new object is created although same content String present in String constant pool (which is created by constant expression on line 3 and referenced by hello reference variable ) so str10 has different reference than hello.
If you compare then It will return true because both have String values of constant expressions which are interned.
Santosh Kumar Nayak wrote:So Produces an object at runtime and it is not in String Pool
Yes It produces a new String object having value "Hello". If you declare str8 and str9 final then becomes compile time constant and reference of String "Hello" from String pool is returned so str10 == hello and str10 == str5 will return true. I had tried different combinations and also drawn an illustration for that, will see If I find in my documentation will post soon.
This is how I had tried with different combinations also had drawn illustration for that. Please bear my hand writing. Output:
one == two: false
one == five: false
one == six: true
one == eight: true
one == ten: false
one == twelve: true