-1

I have below code written:

public class Test{
    int a;
    public static void main(String[] args){
        Test t1 = new Test();
        Test t2 = new Test();
        if(!t1.equals(t2))
            System.out.println("They're not equal");
        if(t1 instanceof Test)
            System.out.println("True");
    }
}

And here is the Output:

They're not equal
True

I even tried to assign the same value to instance variable 'a' of both these objects, like below,

t1.a = 10;
t2.a = 10;

Still the same output.

May I know when the t1.equals(t2) will return True? How does the equals() method work on objects?

atish shimpi
  • 4,873
  • 2
  • 32
  • 50
Touhid K.
  • 351
  • 1
  • 5
  • 23
  • 1
    override the equals method provided by default by the Object class – Sarthak Mittal Dec 11 '14 at 07:24
  • You need to override the methods `equal()` and `hashCode()`(optional) to achieve this –  Dec 11 '14 at 07:25
  • I think you have misunderstood the instance method , please try reading about Object Equality in Java, it would clear all your doubts – Sainath S.R Dec 11 '14 at 07:26
  • 1
    My 2 cents: If you do `Test t1 = new Test(); Test t2 = t1;` then `t1.equals(t2)` will return true. – shree.pat18 Dec 11 '14 at 07:26
  • refer this : http://eclipsesource.com/blogs/2012/09/04/the-3-things-you-should-know-about-hashcode/ – Vishal Zanzrukia Dec 11 '14 at 07:27
  • also this : http://java67.blogspot.in/2013/04/example-of-overriding-equals-hashcode-compareTo-java-method.html – Vishal Zanzrukia Dec 11 '14 at 07:27
  • possible duplicate of [Compare two objects with .equals() and == operator](http://stackoverflow.com/questions/13387742/compare-two-objects-with-equals-and-operator) – Sainath S.R Dec 11 '14 at 07:29
  • possible duplicate of [How to compare two java objects](http://stackoverflow.com/questions/16069106/how-to-compare-two-java-objects) – Raedwald Dec 11 '14 at 07:47

3 Answers3

2

By default, calling equals execute the equals method of Object class, which returns true only when you are comparing an instance to itself.

You can override this method in other classes, so that equals would return true when the properties of both objects are equal.

For example :

@Override
public boolean equals(Object other)
{
    if (other == null)
        return false;
    if (other instance of Test) {
        Test t = (test) other;
        return this.a == t.a;
    }
    return false;
}

Adding this method to your Test class would change the result of t1.equals(t2) to true.

Eran
  • 387,369
  • 54
  • 702
  • 768
1

The Object.equals(Object) Javadoc says (in part),

Indicates whether some other object is "equal to" this one.

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.

For your code you could override equals like

@Override
public boolean equals(Object o) {
    if (o instanceof Test) {
        return ((Test) o).a == a;
    }
    return false;
}
Community
  • 1
  • 1
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
1

The default implementation of Object.equals treats two objects as equal only if they are exactly the same object, not just the same contents but the same reference.

Objects can have different implementations of equals, but you must program them explicitly: if you want to check that all fields are equal, you must actually write an equals implementation that checks that.

Louis Wasserman
  • 191,574
  • 25
  • 345
  • 413