SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
OCPJP (310-065) - 88%
Prithvi Sehgal wrote:Hello Malte,
The snippet
is actually evaluating to foobar which is already present in String Pool constant. So rather then creating a new object,
s3 is pointed towards the already present foo bar. Thats why s1 == s3 prints true.
IMO, thats the reasoning of the output.
BR,
OCPJP (310-065) - 88%
Prithvi Sehgal wrote:IMO,
If you see the snippet it is like
This time as there nothing as foo in the String Pool constant, a new object will be created. In the next line you are just adding bar
into a newly created object. IMO, in this , already a new object was created thats why s4 is already pointing towards a new object.
HTH,
OCPJP (310-065) - 88%
Java Experience
OCPJP 6
Seema Kekre wrote:Hi Malte,
I had the same doubt. Re:String Literals at compile time
Basically is something the compiler sees and checks if it is present in the String literal pool and equates to "foobar" but
In this case, at Line 2, the compiler does not know what the variable s4 holds and is equivalent to creating a new String with the value "foobar", which is different from the String literal value.
Hope this helps.
OCPJP (310-065) - 88%
OCPJP 6
OCPJP (310-065) - 88%
Consider Paul's rocket mass heater. |