tags:

views:

1000

answers:

10

What is the difference between a == b and a.Equals(b)?

+3  A: 

One checks to see if they represent the same object (equals), and the other checks to see if they are the same object (==)

Matt Poush
+3  A: 

This is kind of a duplicate, see here:

http://stackoverflow.com/questions/957533/object-0-object-0/957576#957576

ristonj
+6  A: 

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
TStamper
Which language is this? In C#, unless you write your own `Equals` method, the default one will use reference equality (it's the same as ==)
Orion Edwards
+2  A: 

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.

SLaks
== can't be *overridden* - it can be *overloaded*.
Jon Skeet
Corrected; thanks
SLaks
+23  A: 

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 and b (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 and b 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).

Jon Skeet
There are no virtual methods in java, well no 'virtual' keyword really, every method is what .net call's virtual (can be overridden by a subclass)
Pablo Fernandez
To be more precise, == compares the *values* of two expressions, regardless of whether the expressions are primitive typed or pointers. If those expressions are pointers like "Dog d" or "new Dog()", then what you're testing is "do the expressions point to the same object". The .equals() method is intended to be used to check for semantic equality; do two objects have the same "meaning".
Scott Stanchfield
@Pablo: Try overriding Object.getClass().
Jon Skeet
@Scott: The trouble is things become trickier when you bring in autoboxing etc. If a and b are the same type *and* no overloads are involved, I agree.
Jon Skeet
@Jon: I meant that there's no concept of virtual in Java, some Java programmers might be confused by that. In Java every method is virtual, unless you state otherwise (with 'final'). C# does exactly the opposite, every method is final unless declared 'virtual'
Pablo Fernandez
At least this phrase "In both languages, a.Equals(b) or a.equals(b) will call the virtual Equals/equals method" might sound confusing for any non-net programmer.
Pablo Fernandez
@Pablo: I just regard the virtual/final bit as a matter of different defaults. How the default isn't picked doesn't change the concept. I've always considered non-final methods in Java to be virtual.
Jon Skeet
+2  A: 

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.

Nathan Koop
Except that you can't overload operators in Java.
Jon Skeet
+1  A: 

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.

UncleO
+2  A: 
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
Nicolas Dorier
A: 

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

A: 

Just an interesting detail: in Java == might give the same results as equals() method. Check out when it's true.

Jaroslaw Dobrzanski