-1
import java.io.*;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {

        Dog dog1 = new Dog();
        Dog dog2 = new Dog();

        System.out.println(dog2.equals(dog1));

    }

    public static class Dog{

    }
}

The objects are the same, but equals method says its false. Why? Is it not true? Class is empty. Thanks in advance

2
  • Perhaps you should read the Javadoc of equals method.
    – Eran
    Commented Jun 11, 2017 at 17:49
  • They are not the same object.
    – Joe C
    Commented Jun 11, 2017 at 18:12

5 Answers 5

2

I would advise you to see the documentation: https://docs.oracle.com/javase/7/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).

Because you have not overridden the .equals(Object) method and provided a looser equivalence test, the super-class (i.e., the Object class) version of .equals(...) is called. Because dog1 and dog2 refer to different objects occupying different memory space, by default, they are not equivalent. You must supply your own equivalence test to overcome this.

2
  • But why does it return true? public static void main(String[] args) { // init(); // System.out.println(Staticvar.count); // Dog dog = new Dog(); // System.out.println(); Dog dog1 = new Dog("1"); Dog dog2 = new Dog("1"); System.out.println(dog1.name.equals(dog2.name)); } public static class Dog{ private String name; public Dog(String name){ this.name = name; } } }
    – artek
    Commented Jun 11, 2017 at 17:58
  • @ArtemZaichikov, I would venture to guess that because you are providing object-data in that case, your compiler or IDE is automatically generating a .equals(...) method, which in turn is relying on the .equals(...) method of the String class. I could be mistaken on that, but that seems like the only plausible explanation, in my opinion.
    – Spencer D
    Commented Jun 11, 2017 at 18:06
1

If you don't override the equals method of your Dog class, it falls back on the one from the Object class, that compares object's addresses. You have two instances of Dog, so two different addresses, so they're not equal.

3
  • But "==" for addresses and equals for object's data. I cant understand
    – artek
    Commented Jun 11, 2017 at 17:53
  • @ArtemZaichikov, by default, because the Super Class Object does not know your object data, it performs the most discriminatory test; i.e., it falls back to basically doing a == comparison. The only reason things like Strings and other built-in classes can compare object data with .equals(...) is because they provide override methods, within which code custom to that object type performs the equivalence test on the object data.
    – Spencer D
    Commented Jun 11, 2017 at 17:58
  • @ArtemZaichikov As SpencerD said, the default comportment of equals is the same as ==. You should look at the equals implementation.
    – Turtle
    Commented Jun 11, 2017 at 18:04
0

You should read the javadoc on the Object equals method. These objects are not the same by default behavior of the method; you would need to override it to have it return true.

0

The class Dog does not override the default equals method so the default will apply (which is basically ==) and as dog1 and dog2 are two separate instances of Dog this will be false.

If you want to be able to discriminate in a different way, you should override the equals method and put your comparison logic in it.

0

Everytime you use new keyword while initializing, it means new instance. And two different object instances can be equal or not, depending upon equal definition of your class. The default equals method definition compares memory addresses, which would be different in this case.

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