First comparison should be true (s1 and s2 reference the same object).
Other comparisons should be false (s5, s8, s11, s15 refrence different objects, concatenation invokes String constructor)
I fear it's not that simple though ; - )
Censorship is the younger of two shameful sisters, the older one bears the name inquisition. -- Johann Nepomuk Nestroy
Joined: Sep 16, 2008
The first one is true, that is sure The second and the last one are basicaly the same (whith the exception of the trick I have fallen for , you got me there Sahil) and are both false. The third and fourth ... I'll go with final references beeing as good as string literals, therefore true. As Peter mentioned, concatenation returns a new object, therefore the fourth is false.
According to me s1 & s2 pointing to the same String objects in String pool.
as Strings are immutable for s1 & s2 , there may not be two String objects that containing "SCJP".
s1 & s2 would refer a same location.
answer is as follows and with the following reasons
1. true // both strings refer the to String literal pool object "SCJP"
2. false// this is because when one of the operand is non final, then concatenation is done at run time and hence the + operator creates a new string and hence s1 and s5 refer to different objects
3.true // here both are final hence compile time constants hence concatenation is done at compile time and hence the variable s8 refers to the string in the constant pool referred by s1
4.false// here both the strings are non final and hence not the compile time constants hence the concatenation is done at the run time and hence the "+" operator creates the new string
5.false // the same reason that is for the third S.O.P.
SCJP 6 [86%] June 30th, 2010
OCPWCD [84%] March 26th, 2013
If you find any post useful, click the "plus one" sign on the right