ranjan ranjan - 1 year ago 113
Java Question

String output confusion

I have the below code but why the output is false?

String Str1 = "This is really not immutable!!";
String Str2 = "This is really";
String Str3 = Str2 +"not immutable!!";
System.out.println(Str1 == Str3);


Answer Source

Java does not regard Str3 as a compile-time evaluable constant expression, since Str2 is not marked as final.

If you had written final String Str2 = "This is really "; (note the space) then the string references would have been the same.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download