This week's book giveaway is in the OCAJP forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide 1Z0-808 and have Jeanne Boyarsky & Scott Selikoff on-line! See this thread for details.
As per my understanding the output will be: false true true
is this correct?
Joined: Feb 15, 2007
1) str1 == "Ja"+"va" ---> true, beacuse "Ja" , "va" both String literals(in pool) when concatenated gives "Java" which will be try to create a string in pool. But finds "Java" already created so passes the same reference of str1. thats why true.
2)str1 == s1+s2 ---> false,
Here str1 pointing to String in Pool ---> "Java" in Pool
s1+s2 as we are using the reference , creates a new String in Heap and as usual tries to create another one in pool. but finds "Java" so gives up with creating String in pool. ----> "Java" in Heap(new one)
Hence both references are not equal. so output false.
3)"Java" == "Ja" + "va"
"Java" ---> tries to create one in pool, but gives up as it is already there. and returns the reference of pool string.
"Ja" + "va" --> as explaned in case 1) tries to create "Java" in Pool, and returns the reference of existing pool String.