0

I have a following Question i have two empty Objects and i am using equals() method to compare them, as we know equals method compare contents of an Object, but in this case i dont have any properties to my Object

   Object ob1 = new Object();
   Object ob2 = new Object();
    if(ob1.equals(ob2)){
        System.out.println(" they are Equal");
    }
    else{ 
        System.out.println("not equal");
    }
    if(ob1 == ob2){
        System.out.println(" they are Equal");
    }
    else{ 
        System.out.println("not equal");
    } 

we know == will compare the references of the Objects

what will equals method compare here ??

thanks in Advance ...

6
  • 1
    Sorry - had to vote down because this is clearly answered in the JavaDocs.
    – Nick Holt
    Commented Aug 22, 2013 at 9:41
  • 1
    equals() doesn't compare the contents of the objects as you think it does. It compares objects using the implementation of equals() in the class of the object on which it's invoked. And this equals() method can do various different things depending on the type of the object. In the case of Object, equals() considers two instances equal if they are the exact same instance.
    – JB Nizet
    Commented Aug 22, 2013 at 9:42
  • 1
    It does same thing as == operator. Look at Objest.equals sources.
    – aim
    Commented Aug 22, 2013 at 9:43
  • @aim: no need to look at the sources. It's documented in the javadoc.
    – JB Nizet
    Commented Aug 22, 2013 at 9:44
  • 1
    @JBNizet I'm agree with you :)
    – aim
    Commented Aug 22, 2013 at 9:48

5 Answers 5

3

From the Object.equals JavaDoc:

The equals method for class Object implements the most discriminating possible equivalence relation on objects; that is, for any non-null reference values x and y, this method returns true if and only if x and y refer to the same object (x == y has the value true).

2

It will also compare references, as stated here:

The equals method implements an equivalence relation on non-null object references:

It is reflexive: for any non-null reference value x, x.equals(x) should return true.
It is symmetric: for any non-null reference values x and y, x.equals(y) should return true if and only if y.equals(x) returns true.
It is transitive: for any non-null reference values x, y, and z, if x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) should return true.
It is consistent: for any non-null reference values x and y, multiple invocations of x.equals(y) consistently return true or consistently return false, provided no information used in equals comparisons on the objects is modified.
For any non-null reference value x, x.equals(null) should return false.

1

If both Objects are null then

 if(ob1==null && ob2==null)

If you try to do ob1.equals(null) you will get NullPointerException. Since ob1 it self null.

0
  1. == operator compare reference or memory location of objects in heap, whether they point to same location or not.

  2. equals() method main purpose is two compare the state of two objects or contents of the object.

But you are using Object class equals() method.

Object class default implementation of equals() method work like == ,means it will check the memory reference of the object if they point to same location.

Note: This default implementation is overridden to do content comparision in classes like String,Wrapper classes.

0

While there is some inconsistency about what X.equals(Y) should mean (i.e. what "question" should it answer), two useful questions are:

  • When the object is used as intended, should replacing some references to X with references to Y be expected not to change their behavior?

  • When the object is used as intended, would swapping all references to X with references to Y, and vice versa, be expected not to change their behavior?

If Object defined hashCode() to return the same value for all empty objects, and equals to return true when comparing any two empty objects to each other, those definitions would have satisfied #1. On the other hand, any object which defines equals to test reference equality and hashCode to return identityHashCode will also satisfy #1 (if X and Y are distinct instances, the fact that X.equals(X) is true but Y.equals(X) is false is, in and of itself, good reason to regard the objects as distinct.

While it's debatable whether Object should have been a concrete type, the only use of an instance of Object is as an identity token. As such, that fact that Object is a concrete type implies that its equals methods should test identity.

Not the answer you're looking for? Browse other questions tagged or ask your own question.