Comparing java Strings with == [duplicate]

限于喜欢 提交于 2019-12-14 03:30:18

问题


Possible Duplicate:
Java String.equals versus ==

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

Why do I often see, that equals() method is used instead?

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


回答1:


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()].




回答2:


== 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.




回答3:


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).




回答4:


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#




回答5:


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.)




回答6:


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




回答7:


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.




回答8:


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.




回答9:


== 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.




回答10:


== 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.




回答11:


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


来源:https://stackoverflow.com/questions/6940521/comparing-java-strings-with

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!