This week's book giveaway is in the OCMJEA forum. We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line! See this thread for details.
Let's walk through it line by line (well, almost).
Any object must be equal to itself. This is just a shortcut that will prevent checking all fields if you're comparing an object to itself (as it's fields will be equal of course). This check can be skipped but is mostly included for (slightly) better speed.
Any object will never be equal to null. Pretty clear.
Now this is a bit interesting. Most people use the instanceof operator to determine if an object is an instance of the current class (e.g. obj instanceof Date). However, that may break the symmetry rule for Object.equals: if x.equals(y) then y.equals(x). An example using instanceof will show how this can be broken:
Now assume we have a Point p and a ColorPoint cp. Both have x == 0 and y == 0. cp has a red color. Now p.equals(cp) will return true -- cp is also a Point and has the same x and y values. However, cp.equals(p) will return false. Sure, the x and y values are the same, but p is not a ColorPoint.
This is where the getClass() comparison comes in place. Even if you call getClass() inside Point.equals, it will return the Class object for ColorPoint if called in cp.equals(p).
In p.equals(cp) the check will be if Point.class == ColorPoint.class, which returns false. In cp.equals(p) the check will be if ColorPoint.class == Point.class, again false. So now p.equals(cp) and cp.equals(p) will both return false.
This is simple casting to the class and checking one field, which is allowed to be null. If getIdioma() returns null for both or they are equal the method will now return true; otherwise it will return false.
I have one suggestion though: either use the field (idIdioma) or the method (getIdIIdioma()), but not both. If a sub class overrides this method to return something else you have two different checks where there should be one.