开发者

Override equals method properly

开发者 https://www.devze.com 2023-03-13 08:28 出处:网络
my teacher gave me the solution of a equals overriding example and it goes like this: @Ov开发者_Go百科erride

my teacher gave me the solution of a equals overriding example and it goes like this:

@Ov开发者_Go百科erride
public boolean equals(Object o)
{
    if (this == o) return true;
    boolean result = false;
    if (o instanceof Product)
    {
        Product other = (Product)o;
        result = this.id == other.id;
    }
    return result;
}

the method is overrided for Product class,which have an attribute id which is unique for each product. But I don't understand the meaning of the first if, I think that the second if already check the restrictions of the first one. Can anyone give me an example of this code working, and this one below not? Thanks!

@Override
public boolean equals(Object o)
{
    boolean result = false;
    if (o instanceof Product)
    {
        Product other = (Product)o;
        result = this.id == other.id;
    }
    return result;
}


Both of the code examples work. The if (this == o) return true; in the first example is a performance optimization (most probably premature optimization - always profile first), which checks whether the object is being compared to itself. In Java the == operator compares whether two objects are the same instance, not whether they are different instances with the same data.

There are may styles of writing the equals method. Here is how I usually do it:

public boolean equals(Object obj) {
    if (obj instanceof Product) {
        Product that = (Product) obj;
        return this.id == that.id;
    }
    return false;
}

If I know that my code will never compare the object against objects of other types, or against null, then I may even write the code as shown below, so that it will throw an exception if that anyways happens - it would mean that my code has a bug, so by failing early I will find out about it and fix it sooner.

public boolean equals(Object obj) {
    Product that = (Product) obj;
    return this.id == that.id;
}


You are right. The first if-statement is redundant since this == o implies o instanceof Product and this.id == other.id.

If the argument is performance, I'd say it smells premature optimization.


if (this == o) return true;

The above statement is redundant.

More specifically, it's simply checking to see if you are comparing an object to itself ... so it can skip the code below it. For example:

Foo f = new Foo();
f.equals(f); //the if (this == o) would be true. References the same thing.

Note: As an aside, if one overrides equals, one should override hashcode() to maintain the general contract for hashcode() - equal objects must have the same hashcode (the reverse is not true since two objects could have the same hash but not be equal.)

http://download.oracle.com/javase/6/docs/api/java/lang/Object.html#equals(java.lang.Object)


In eclipse you have the option to "Generate hashCode() and equals()..." (menu Source)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号