June 2018
Beginner
722 pages
18h 47m
English
For such a comparison of objects—by the value of their fields—the equals() method is to be used. In Chapter 2, Java Language Basics, we already established that all reference types extend (implicitly) the java.lang.Object class, which has the equals() method implemented:
public boolean equals(Object obj) { return (this == obj);}
As you can see, it compares only references using the equality operator, which means that if a class or one of its parents does not implement the equals() method (which overrides the implementation of the Object class), the result of using the equals() method will be the same as using the equality operator ==. Let's demonstrate this. The following class does not implement ...
Read now
Unlock full access