开发者

Comparing java Strings with == [duplicate]

开发者 https://www.devze.com 2023-03-26 00:37 出处:网络
This question already has answers here: Closed 11 years ago. Possible Duplicate: Java String.equals versus ==
This question already has answers here: Closed 11 years ago.

Possible Duplicate:

Java String.equals versus ==

Is it possible to compare Java Strings using == operator?

Why do I often see, that equals(开发者_Python百科) method is used instead?

Is it because when comparing with literal Strings (like "Hello") using == doesn't imply calling equals()?


there is no custom operator overloading in java. [so you cannot overload it to call equals()]

the equals() ensures you check if 2 Objects are identical,while == checks if this is the exact same object. [so no, using == does not invoke equals()].


== checks if the two objects refer to the same instance of an object, whereas equals() checks whether the two objects are actually equivalent even if they're not the same instance.


No, it's not possible, because with == you compare object references and not the content of the string (for which you need to use equals).


In Java, you cannot overload operators. The == operator does identity equality. The equals(...) method, on the other hand can be be overridden to do type-specific comparisons.

Here's a code snippet to demonstrate:

String a = "abcdef";
String b = a;
String c = new String(a);

println(a == b); // true
println(a.equals(b)); // true

println(a == c); // false
println(a.equals(c)); // true

The one complication is with equals(...) you need to care about null, too. So the correct null-safe idiom is:

(a == null ? b == null : a.equals(b))

This is a loop you don't have to jump through in say C#


To expand on @amit's answer, the == operator should only be used on value types (int, double, etc.) A String is a reference type and should therefore be compared with the .equals() method. Using the == operator on a reference type checks for reference equality in java (meaning both object references are pointing to the same memory location.)


String is a class.So if you try to compare a String with its object that holding a string value you can't use == as it is looking for an object.For comparing the contents of the object you have to use equals


Operator == compares for string object references ,whereas String.equals method checks for both object references + object values . Moreover , String.equals method inturn uses == operator inside its implementation.


From what I know the '==' operator is used to check whether or not to objects are identical.
The presumable compared strings might have the same value(nr of chars etc), but be in fact two totally different objects, thus rendering the comparison false.


== returns true if the memory address is equal on both sides, except for primitive types.

equals should be used on everything that isn't a primitive. classes for the main part.


== operator checks the bit pattern of objects rather than the contents of those objects, but equals function compare the contents of objects.

String str1=new String("abc");
String str2=new String("abc");

System.out.println(str1==str2); will return false because str1 and str2 are different object created with "new" . System.out.println(str1.equals(str2)) will return true because equals() checks for contents of object.


As amit already said, == checks for being the same object whereas equals() checks for the same content (ok, the basic implementation is equal to == but String overrides this).

Note:

"Hello" == "Hello" //most probably would be true
"Hello".equals( "Hello" ) //will be true

String s1, s2; //initialize with something different than a literal, e.g. loading from a file, both should contain the same string
s1 == s2 //most probably will NOT be true
s1.equals( s2) //will be true, if both contain the same string, e.g. "Hello"

Besides that, the same holds true for object wrappers of primitives, e.g.

 Long l1 = 1L;
 Long l2 = 1L;
 l1 == l2 //will most likely be true for small numbers, since those literals map to cached instances
 l1.equals(l2) //will be true

 new Long(1) == new Long(1) //will NOT be true
 new Long(1).equals(new Long(1)) //will be true
0

精彩评论

暂无评论...
验证码 换一张
取 消