比较 java 字符串与 ==
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6940521/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Comparing java Strings with ==
提问by Alexander Farber
Possible Duplicate:
Java String.equals versus ==
可能的重复:
Java String.equals 与 ==
Is it possible to compare Java Strings using == operator?
是否可以使用 == 运算符比较 Java 字符串?
Why do I often see, that equals() method is used instead?
为什么我经常看到,用equals()方法来代替?
Is it because when comparing with literal Strings (like "Hello") using == doesn't imply calling equals()?
是因为当使用 == 与文字字符串(如“Hello”)进行比较时,并不意味着调用 equals()?
回答by amit
there is no customoperator overloading in java. [so you cannot overload it to call equals()]
java中没有自定义运算符重载。[所以你不能重载它来调用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()].
equals() 确保您检查 2 个对象是否相同,而 == 检查这是否是完全相同的对象。[所以不,使用 == 不会调用 equals()]。
回答by BlackHyman
==
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.
==
检查两个对象是否引用对象的同一个实例,而equals()
检查两个对象是否实际上等效,即使它们不是同一个实例。
回答by Joachim Rohde
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).
不,这是不可能的,因为使用 == 您比较对象引用而不是字符串的内容(您需要使用 equals)。
回答by Dilum Ranatunga
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.
在 Java 中,您不能重载运算符。该==
运营商不认同平等。equals(...)
另一方面,该方法可以被覆盖以进行特定于类型的比较。
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:
一个复杂的问题是equals(...)
你也需要关心空值。所以正确的空安全习语是:
(a == null ? b == null : a.equals(b))
This is a loop you don't have to jump through in say C#
这是一个循环,你不必在 C# 中跳过
回答by netbrain
==
returns true if the memory address is equal on both sides, except for primitive types.
==
如果双方的内存地址相等,则返回 true,原始类型除外。
equals should be used on everything that isn't a primitive. classes for the main part.
equals 应该用于所有不是原始类型的东西。主要部分的类。
回答by medkg15
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.)
为了扩展@amit 的答案,== 运算符应仅用于值类型(int、double 等)。 String 是引用类型,因此应与 .equals() 方法进行比较。在引用类型上使用 == 运算符检查 java 中的引用相等性(意味着两个对象引用都指向相同的内存位置。)
回答by Rasel
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
String 是一个类。因此,如果您尝试将 String 与其持有字符串值的对象进行比较,则不能使用 ==,因为它正在寻找一个对象。为了比较对象的内容,您必须使用 equals
回答by Sandeep Pathak
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.
运算符 == 比较字符串对象引用,而 String.equals 方法检查对象引用 + 对象值。此外,String.equals 方法在其实现中又使用 == 运算符。
回答by Radu
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.
据我所知,'==' 运算符用于检查对象是否相同。
假定的比较字符串可能具有相同的值(字符数等),但实际上是两个完全不同的对象,从而使比较为假。
回答by Android Killer
== operator checks the bit pattern of objects rather than the contents of those objects, but equals function compare the contents of objects.
== 运算符检查对象的位模式而不是这些对象的内容,但 equals 函数比较对象的内容。
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.
System.out.println(str1==str2); 将返回 false 因为 str1 和 str2 是用 "new" 创建的不同对象。System.out.println(str1.equals(str2)) 将返回 true,因为 equals() 检查对象的内容。