Reputation: 2493
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
Upvotes: 5
Views: 1565
Reputation: 1504
Because + has higher priority compare to = and if you use bracket(str1 == str2) then this result give true because highest priority is (. So First it checks bracket inside data.
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));
Output:
Using equals() str1 and str2 Equals :true
Using == str1 and str2 Equals :true
Upvotes: 4
Reputation: 607
equals method returns true if and only if x and y refer to the same object.Follwoing is the Object class implementation of equals method.
public boolean equals(Object obj) {
return (this == obj);
}
In String class this method has overridden as following.
public boolean equals(Object anObject) {
if (this == anObject) {
return true;
}
if (anObject instanceof String) {
String anotherString = (String)anObject;
int n = count;
if (n == anotherString.count) {
char v1[] = value;
char v2[] = anotherString.value;
int i = offset;
int j = anotherString.offset;
while (n-- != 0) {
if (v1[i++] != v2[j++])
return false;
}
return true;
}
}
return false;
}
And if you use == operator it just check both references are having same object. Similar to the Object class equals method.
Upvotes: 0
Reputation: 5954
In Java ==
operator matches the two objects i.e their address while .equals()
method mathces the values of both objects, thats why you are getting true
for equals()
and false
for ==
as both are different objects.
Upvotes: -1
Reputation: 815
See http://bmanolov.free.fr/javaoperators.php for a table of operator precedence in Java.
The + operator is higher precedence than the == operator.
So, in effect, your code is equivalent to the following:
System.out.println( ("Using == str1 and str2 Equals :" + str1) == str2);
Note the placement of the parentheses that I added. It evaluates to this:
System.out.println( (str_x + str1) == str2);
And then to this:
System.out.println( str_y == str2 );
And then to this:
System.out.println( false );
In order to get the result you want, you must use parentheses to specify that you want the == operator to be resolved BEFORE the + operator:
System.out.println( "Using == str1 and str2 Equals :" + (str1 == str2));
Notice the new placement of the parentheses.
Upvotes: 7
Reputation: 2795
str1.equals(str2)
returns true
because the equals()
function compares the content of the string variables, where as ==
operator compares the instances. Since str1
and str2
are two differences of instances of String
class, it returns false
Upvotes: -1
Reputation: 8598
Try surrounding it with () like this:
System.out.println("Using == str1 and str2 Equals :" + (str1 == str2));
Upvotes: 2
Reputation: 11958
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.
Upvotes: 12
Reputation: 3295
The reason is that you cannot compare strings in Java using ==
.
In C++ or C# (or other languages supporting operator redefinition), you can overwrite the ==
operator to provide that functionality. Java does not support that.
Upvotes: -1
Reputation: 1751
==
can only be used to compare primitive datatypes. To compare objects you need to use equals
method. Using a ==
operator on objects actually compares there addresses instead of values.
Upvotes: -2
Reputation: 13097
Maybe an order of operations thing? Try:
System.out.println("Using == str1 and str2 Equals :" + (str1 == str2));
Upvotes: 2