0
public class Test {
    public static void main(String[] args) {
        Object o1 = new Object();
        Object o2 = new Object();
        System.out.print((o1 == o2) + " " + (o1.equals(o2)));
    }
}

I read this in a different answer:

The == operator tests whether two variables have the same references (aka pointer to a memory address).

Whereas the equals() method tests whether two variables refer to objects that have the same state (values).

Here, since o1 and o2 reference two different objects, I get why == returns false.

But both the objects are created using the default constructor of the Object class, and therefore have same values. Why does the equals() method return false?

Community
  • 1
  • 1
Enzio
  • 799
  • 13
  • 32
  • Maybe because one has to **override the `equals` method? – Willem Van Onsem Mar 27 '17 at 14:17
  • 5
    See [Object.equals](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#equals-java.lang.Object-) "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)." If you want something less discriminating, you have to `override` equals and alter the behaviour. – khelwood Mar 27 '17 at 14:17
  • `Object.equals` uses `==` as comparator. – Oneiros Mar 27 '17 at 14:20
  • 1
    Objects are only considered equal if they have the same reference. That was just a design decision because otherwise every object would be equal which would create a whole bunch of problems. – OH GOD SPIDERS Mar 27 '17 at 14:23
  • @Downvoters, do bear in mind that this question is well-written, with a code snippet, and documented and expected output. It's difficult to search for these things if you don't know the terminology. – Bathsheba Mar 27 '17 at 14:24

3 Answers3

6

The implementation of equals() supplied by java.lang.Object is defined to return false, unless the references refer to the same object, in which case it returns true.

This is by design (the method mimics the behaviour of ==) and encourages programmers to implement their own version of equals(), if appropriate, for their class. For example, see java.lang.String#equals which compares the contents if another String is passed as an argument.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
3

You have to write your own equals method that overrides the equals method of class Object because that method returns true if this object is the same as the object in the argument and false otherwise.

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). (for more information, read Javadoc)

kkica
  • 4,034
  • 1
  • 20
  • 40
2

All Java objects inherit from the Object class. The methods of Object, therefore, are available to all Java objects. One of these methods is equals().

The implementation for equals() in class Object, by default, is identical to the == operator.

If a programmer wishes to use equals() to test objects for value equality, he must override equals() and provide his own implementation (that should comply with the general contract for equals(); refer to the Javadoc).

scottb
  • 9,908
  • 3
  • 40
  • 56