strange behavior of == in Java

Raj :

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

shift66 :

it's the same as ("Using == str1 and str2 Equals :" + str1) == str2 and this is false, of course. Expression is parsed from left to right and so at first it concatenates "Using == str1 and str2 Equals :" and str1, then applies == operator.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related