What is the difference between a == b
and a.Equals(b)
?
views:
1000answers:
10One checks to see if they represent the same object (equals), and the other checks to see if they are the same object (==)
This is kind of a duplicate, see here:
http://stackoverflow.com/questions/957533/object-0-object-0/957576#957576
The == operator checks to see if two objects are exactly the same object which is not the way to go in most cases. The Equals method will be able to compare both the object internally
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
It depends on the types of a
and b
.
In particular, Equals is a virtual method, so that its behavior doesn't depend on the compile-time types of a and b.
In Java, == will always compare by reference, which is not necessarily what you want, especially for strings.
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 ==
.
Also, a.Equals(b) will throw a NullReferenceException
(NullPointerException
in Java) if a is null
.
Assuming the types of a
and b
are reference types:
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-time types 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 equal values and it would still return true).
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 type rather than the compilation-time type that overload resolution depends on.
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)
.
Eric Lippert has a great blog post on this. He speaks specifically about C#, but I'd imagine the reasons for java would be the same or quite similiar.
a==b returns true if the references contain the same value, i.e., they point to the same object, or they are both null.
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.
o.equals() will throw an exception if o is a null reference.
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
static bool Object.operator==(object a, object b) //Reference comparison
whereas a == b use
static bool String.operator==(string a, string b) //String comparison
but a.Equals(b) or aAsObj.Equals(b) always use :
bool String.Equals(Object obj) //String comparison
== 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
Just an interesting detail: in Java == might give the same results as equals() method. Check out when it's true.