I have two questions:
public static void main(String[] args) {
String s1 = "bla";
String s2 = "b" +"l" + "a";
String s3 = "b".concat("l").concat("a");
if(s1 == s2)
System.out.println("Equal");
else
System.out.println("Not equal");
if(s1 == s3)
System.out.println("Equal");
else
System.out.println("Not equal");
}
Why does
s1
ands2
point to the same object, whereass1
ands3
doesn't? (There is no usage ofnew
keyword).If I get a string from the user and add to the above code these lines:
BufferedReader in=new BufferedReader(new InputStreamReader(System.in)); String name=in.readLine(); if(name.equals("test")) s1 = s1 + "xyz";
If the user enters
xyz
the program will printNot equal
, when the user enters another thing the program outputsEqual
. Does this mean that the pool changes through the execution of the whole program? Does the optimizer works at the compile time and continues to work in theruntime
?