I observed a strange behavior == operator in java. I am trying to print the out put as follows
String str1 = "Rajesh";
String str2 = "Rajesh";
System.out.println("Using equals() str1 and str2 Equals :"
+ str1.equals(str2));
System.out.println("Using == str1 and str2 Equals :"
+ str1 == str2);
The first SOP statement printing
Using equals() str1 and str2 Equals :true
and the next SOP printing only false .
I tried compiling in both eclipse and Net Beans but result is the same .
I am so confused why
Using == str1 and str2 Equals :
is not printing
Help me out in this
Thanks in advance,
Raj
it’s the same as
("Using == str1 and str2 Equals :" + str1) == str2and this is false, of course. Expression is parsed from left to right and so at first it concatenates"Using == str1 and str2 Equals :"andstr1, then applies==operator.