C# 等于/等于和 == 运算符之间的区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/971954/
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
Difference between Equals/equals and == operator?
提问by
What is the difference between a == b
and a.Equals(b)
?
a == b
和 和有a.Equals(b)
什么区别?
回答by TStamper
The ==operator checks to see if two objects are exactly the same object which is not the way to go in most cases. The Equalsmethod will be able to compare both the object internally
在==操作符检查是否两个对象是完全一样的对象,它是不是在大多数情况下要走的路。该的Equals方法将能够在内部比较两种对象
Example:
例子:
class Mycar
{
string color;
Mycar(string str)
{
color = str;
}
}
Mycar a = new Mycar("blue");
Mycar b = new Mycar("blue");
a==b // Returns false
a.Equals(b) // Returns true
回答by SLaks
It depends on the types of a
and b
.
这取决于类型a
和b
。
In particular, Equals
is a virtual method, so that its behavior doesn't depend on the compile-time types of a and b.
特别是,Equals
是一个虚方法,因此它的行为不依赖于 a 和 b 的编译时类型。
In Java, ==
will always compare by reference, which is not necessarily what you want, especially for strings.
在 Java 中,==
总是会按引用进行比较,这不一定是您想要的,尤其是对于字符串。
In C#, ==
can be overloaded, but is not virtual (it's a static
method). Therefore, if a
or b
are declared as object
, it will compare by reference, even if their actual type overloads operator ==
.
在 C# 中,==
可以重载,但不是虚拟的(它是一种static
方法)。因此,如果a
或b
被声明为object
,它将通过引用进行比较,即使它们的实际类型重载operator ==
。
Also, a.Equals(b)
will throw a NullReferenceException
(NullPointerException
in Java) if a is null
.
此外,如果 a 是,a.Equals(b)
则会抛出NullReferenceException
(NullPointerException
在 Java 中) null
。
回答by Jon Skeet
Assuming the types of a
and b
are reference types:
假设的类型a
和b
是引用类型:
In Java, == will always compare for identity- i.e. whether the two values are references to the same object. This is also called reference equality. Java doesn't have any user-defined operator overloading.
In C# it depends. Unless there's an overloaded operator which handles it, == will behave like Java (i.e. comparing for reference equality). However, if there's an overload which matches the compile-timetypes of
a
andb
(e.g. if they're both declared as strings) then that overload will be called. That can behave how it wants, but it typically implements value equality(i.e.a
andb
can refer to different but equalvalues and it would still return true).
在 Java 中, == 将始终比较标识- 即两个值是否是对同一对象的引用。这也称为引用相等。Java 没有任何用户定义的运算符重载。
在 C# 中,这取决于。除非有一个处理它的重载运算符,== 将表现得像 Java(即比较引用相等)。但是,如果存在与and的编译时类型匹配的重载(例如,如果它们都声明为字符串),则将调用该重载。这可以按照它想要的方式运行,但它通常实现值相等(即,可以引用不同但相等的值,但仍会返回 true)。
a
b
a
b
In both languages, a.Equals(b)
or a.equals(b)
will call the virtual Equals
/equals
method declared by Object
, unless a more specific overload has been introduced by the compile-time type of a
. This may or may not be overridden in the execution-time type of the object that a
refers to. In both .NET and Java, the implementation in Object
also checks for identity. Note that this depends on the execution-time typerather than the compilation-time typethat overload resolution depends on.
在这两种语言中,a.Equals(b)
或a.equals(b)
将调用由声明的虚拟Equals
/equals
方法Object
,除非a
. 这可能会或可能不会在a
引用的对象的执行时类型中被覆盖。在 .NET 和 Java 中,实现Object
也检查身份。请注意,这取决于执行时类型,而不是重载解析所依赖的编译时类型。
Of course, if a
is null
then you'll get a NullReferenceException
/NullPointerException
when you try to call a.equals(b)
or a.Equals(b)
.
当然,如果a
是,null
那么当你尝试调用or时你会得到一个NullReferenceException
/ 。NullPointerException
a.equals(b)
a.Equals(b)
回答by UncleO
a == b
returns true if the references contain the same value, i.e., they point to the same object, or they are both null.
a == b
如果引用包含相同的值,即它们指向同一个对象,或者它们都为空,则返回 true。
The equals()
method can be overridden to compare objects. For example, on Strings
, the method returns true
if the strings contain the same string, even if they are different string objects. You can do similar things with your own objects.
equals()
可以重写该方法以比较对象。例如, on Strings
,true
如果字符串包含相同的字符串,即使它们是不同的字符串对象,该方法也会返回。你可以用你自己的对象做类似的事情。
o.equals()
will throw an exception if o is a null reference.
o.equals()
如果 o 是空引用,则会抛出异常。
回答by Nicolas Dorier
String a = "toto".Substring(0, 4);
String b = "toto";
Object aAsObj = a;
Assert.IsTrue(a.Equals(b));
Assert.IsTrue(a == b);
Assert.IsFalse(aAsObj == b);
Assert.IsTrue(aAsObj.Equals(b));
This test pass in .NET, the trick is that Equals
is a method, whereas ==
is a static
method, so aAsObj == b
use
这个测试在 .NET 中通过,诀窍是它Equals
是一个方法,而它==
是一个static
方法,所以aAsObj == b
使用
static bool Object.operator==(object a, object b) //Reference comparison
whereas a == b
use
而a == b
使用
static bool String.operator==(string a, string b) //String comparison
but a.Equals(b)
or aAsObj.Equals(b)
always use :
但a.Equals(b)
或aAsObj.Equals(b)
总是使用:
bool String.Equals(Object obj) //String comparison
回答by Nicolas Dorier
==
uses the reference of an object, or if an integer/float etc, then it compares the actual number. Technically it just compares what in the memory location.
Whereas .equals
uses a method inside the object class to compare objects, it can be overridden for your individual classes.
Also as arrays also deal with references, its also helpful not to use array1[i] = array2[i]
, use arraycopy
or clone()
.
I think .equals
can also be used with arrays
==
使用对象的引用,或者如果是整数/浮点数等,则比较实际数字。从技术上讲,它只是比较内存位置中的内容。虽然.equals
使用对象类内部的方法来比较对象,但可以为您的各个类覆盖它。此外,由于数组也处理引用,因此不使用array1[i] = array2[i]
、 使用arraycopy
或也很有帮助clone()
。我认为.equals
也可以与数组一起使用
回答by Ravi K.
==
checks the Object reference, basically it compares the hashcodes. Equals uses the contents in the object. Remember, we have to override the .equals
method accordingly in our class.
==
检查对象引用,基本上它比较哈希码。Equals 使用对象中的内容。请记住,我们必须.equals
在我们的类中相应地覆盖该方法。
回答by Kumar KL
==, It only returns a value of hash code according to their addresses so the different addresses even though the strings or any data which r similar also it returns false....THis s help full for the conditions ,returns boolean value.
==,它只根据它们的地址返回一个哈希码的值,所以不同的地址即使字符串或任何类似的数据也返回false......这对条件有帮助,返回布尔值。
回答by akkhatri
Suppose we have a
and b
or two different objects and we want to compare these two object references. Then we use the ==
operator and when using a.equals(b)
, it compares the string values.
假设我们有a
和b
或两个不同的对象,我们想比较这两个对象引用。然后我们使用==
运算符,当使用时a.equals(b)
,它比较字符串值。
回答by subhashis
== is a fundamental operator in the language. The operator == tests to see if two object reference variables refer to the exact same instance of an object.
== 是语言中的基本运算符。运算符 == 测试以查看两个对象引用变量是否引用了完全相同的对象实例。
equals () is an instance method which is fundamentally defined by the java.lang.Object class. The method, .equals() tests to see if the two objects being compared to each other are equivalent , but they need not be the exact same instance of the same object.
equals() 是一个实例方法,基本上由 java.lang.Object 类定义。方法 .equals() 测试以查看相互比较的两个对象是否等效,但它们不必是同一对象的完全相同的实例。
The == operator always gives you the same result, but the equals () method gives you output according to your implementation (implemented logic).Proper overriding of equals: Considerations' whenever overriding equals () method.
== 运算符始终为您提供相同的结果,但 equals () 方法根据您的实现(实现的逻辑)为您提供输出。正确覆盖 equals: 每当覆盖 equals () 方法时的注意事项。
1. Reflexive: For any non-null reference x, x.equals(x) should return true.
1. 自反:对于任何非空引用 x,x.equals(x) 应该返回 true。
2. Symmetric: For any non-null reference x and y, if x.equals(y) is true then y.equals(x) must return true.
2. 对称:对于任何非空引用 x 和 y,如果 x.equals(y) 为真,则 y.equals(x) 必须返回真。
3. Transitive: For any non-null reference x , y and z, if x.equals(y) is true, y.equals(z) is true then x.equals(z) must return true.
3. 传递性:对于任何非空引用 x、y 和 z,如果 x.equals(y) 为真,y.equals(z) 为真,则 x.equals(z) 必须返回真。
4. Consistent: For any non-null reference x and y, multiple invocations of x.equals(y) consistently return true or consistently return false, without changing the information provided for the equals comparisons.
4. 一致:对于任何非空引用 x 和 y,多次调用 x.equals(y) 始终返回 true 或始终返回 false,而不会更改为 equals 比较提供的信息。
5. For any non-null reference x, x.equals (null) must return false. NOTE: If o1.equals (o2) is true then o1.hashcode () ==o2.hashcode (), but the reverse may or may not be true.
5.对于任何非空引用x,x.equals(null)必须返回false。注意:如果 o1.equals(o2) 为真,则 o1.hashcode() ==o2.hashcode(),但反过来可能是也可能不是。
Examples:
例子:
Integer i = new Integer(10); Integer j = i;
整数 i = 新整数(10); 整数 j = i;
in the above code. i == j is true because both i and j refer to the same object.
在上面的代码中。i == j 为真,因为 i 和 j 指的是同一个对象。
Integer i = new Integer (10); Integer j = new Integer(10);
整数 i = 新整数 (10); 整数 j = 新整数(10);
In the above code, i == j is FALSE because, although they both have the value 10, they are two different objects. But i.equals (j) will return true.
在上面的代码中, i == j 是 FALSE,因为虽然它们的值都是 10,但它们是两个不同的对象。但是 i.equals (j) 将返回 true。
Using Auto-boxing
使用自动装箱
Integer i = 10;
Integer j = 10;
Boolean b = (i == j);
System.out.println (b);
整数 i = 10;
整数 j = 10;
布尔值 b = (i == j);
System.out.println(b);
This will return TRUE because integers between ranges -127 to 128 be pooled, so in this case both are same objects (JVM not going to create a new object it will retrieve it from pool).
这将返回 TRUE,因为范围 -127 到 128 之间的整数被合并,因此在这种情况下,两者都是相同的对象(JVM 不会创建新对象,它将从池中检索它)。
String class overrides the equals method, so here is an example of equals vs. == String s1 = new String ("abc"); String s2 = new String ("abc");
String 类覆盖了 equals 方法,所以这里有一个 equals vs. == String s1 = new String ("abc") 的例子;String s2 = new String("abc");
NOTE: Strings are created in String constant pool so when we create like String s=”abc” it will check the pool by invoking the native method intern (), for its existence in the existing pool if it did not found any String then it will create new one but if we invoke new operator then it will create one new String irrespective of checking the pool for existence.
注意:字符串是在字符串常量池中创建的,因此当我们创建像 String s=”abc” 这样的字符串时,它将通过调用本地方法 intern () 来检查池,如果它没有找到任何字符串,则它是否存在于现有池中将创建一个新的,但如果我们调用 new 运算符,那么它将创建一个新的字符串,而不管是否检查池是否存在。
public class StringEqualityTest {
public static void main(String []a){
String s1=new String("abc");
String s2=new String("abc");
System.out.print("s1.equals(s2) :"+s1.equals(s2)+" s1==s2 :");
System.out.println(s1==s2);
String s3="abc";
String s4="abc";
System.out.print("s3.equals(s4) :"+s1.equals(s2)+" s3==s4 :");
System.out.println(s3==s4);
}
}
OUTPUT: s1.equals(s2) :true s1==s2 :false s3.equals(s4) :true s3==s4 :true
输出: s1.equals(s2) :true s1==s2 :false s3.equals(s4) :true s3==s4 :true