ranjan ranjan - 3 months ago 21
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);

Op:-false

Answer

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.

Comments