In Jaworski's
applet I found the following question:
//what is the output displayed by the following program?
class Question
{
public static void main(
String [] args)
{
String s1 = "ab";
String s2 = "abcd";
String s3 = "cd";
String s4 = s1 + s3;
s1 = s4;
System.out.println("s1"+((s1 == s2)? "==" : "!=")+" s2");
}
}
The answer is: s1 != s2
Apparently, after concatenating s1 and s3, the reference s4 does not refer to a literal in the string pool.
However, if you replace
String s4 = s1 + s3;
with
String s4 = "ab" + "cd";
the result is: s1 == s2
Apparently, the concatenation of these two string literals again results in a reference string literal. Does anybody know what's going on?
------------------