Override method correctly

my teacher gave me a solution to an equivalent example, and it looks like this:

@Override 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 overridden for the Product class, which has an attribute identifier that is unique to each product. But I do not understand the meaning of the first, if, I think, the second, if you already check the limitations of the first. Can someone give me an example of this code that works, and this one below? 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; } 
+4
source share
5 answers

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

There are writing styles for the equals method. This 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 an object with objects of other types or against null, then I can even write the code as shown below to throw an exception if this happens one way or another - this will mean that my code there is a mistake, therefore, not having managed 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; } 
+4
source

Read the effective chapter 3 of the chapter available online at http://java.sun.com/developer/Books/effectivejava/Chapter3.pdf

It is very important to understand this well.

+3
source

You're 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 would say that it smells like premature optimization.

0
source
 if (this == o) return true; 

The above statement is redundant.

In particular, it just checks to see if you are comparing an object with itself ... so that it can skip the code under it. For instance:

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

Note. If you leave aside, if one overrides the equal, you should redefine hashcode () to maintain a common contract for hashcode () - equal objects must have the same hash code (the reverse is not true, since two objects can have the same hash, but not equal.)

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

0
source

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

0
source

All Articles