The code in the following snippet just compares string references.
String str1 = "mystring9";
String str2 = "mystring"+String.valueOf(9);
System.out.println(str1==str2);
In this case, str1==str2
returns false
.
The following segment of code also returns false
.
String str1 = "mystring9";
String str2="mystring"+str1.length();
System.out.println(str1==str2);
The following code however, returns true
.
String str1 = "mystring9";
String str2 = "mystring"+9;
System.out.println(str1==str2);
I think, the expression "mystring"+9
in this code should internally be evaluated to String.valueOf(9)
though why do the first two examples return different output than the preceding example?