Questions
The class Object defines an equals() method to compare objects. Specify the advantages and disadvantages of using this method and suggest an alternative for equality.
1. Advantages and disadvantages of using equals()
According to the description in the Java 8 APIs, equals() is a method of the Object class, defined as follows:
1 | equals(): |
1.1. Advantages
Because all subclasses inherit the Object class by default, all subclasses can call the equals() method in the Object class. Therefore, it is convenient to use the equals method directly to compare whether two objects have the same reference in a subclass, which is an advantage. In addition, in some subclasses, such as String, this class overrides the equals() method in the Object class. You can directly call equals() to compare whether the contents of two string objects are the same, so it is also very convenient. This is another benefit.
Codes:
1 | package discussionassignment; |
Outputs:
1 | false |
As shown in the above example, the reference in str1 points to “hello” in the constant pool, and the reference in str2 points to “hello” in a heap. Therefore, if we use “==“ to compare the two references, the output is false because the references are not the same. However, when comparing two strings, we expect to compare the contents, not the references. Hence, using the equals() method is more proper. Because the String class overrides the equals() method of the Object class, we can use this method to compare the contents instead of the reference. Hence, the result of str1.equals(str2) is true, which is what we expect.
1.2. Disadvantages
Although all classes will automatically inherit the equals() method in the Object class, not all classes override the equals() method. Therefore, when using the equals() method to determine if the two objects are equal, it can only judge whether the references of the two objects are equal. However, sometimes, we need to judge the content stored in the object instead of the reference. Therefore, this is the shortcomings of the equals() method.
Codes:
1 | public class Student { |
Outputs:
1 | false |
For instance, in the above example, student1 and student2 have the same student number, 0001, so, in fact, they are the same student. Therefore, we expect the result of student1.equals(student2) to be true. However, since the Student class does not override the equals() method, equals() compares the reference of student1 and student2 instead of the student number, and the output is false, which is not what we expect. Hence, this is the disadvantage of the equals() method.
2. The alternative for equality
There are three ways to replace equals(). First, use “==” to replace equals(). However, “==“ can only be used to determine if the two objects have the same reference instead of the contents. Second, implement the Comparable interface and override the compareTo() method. Third, the implements Comparator interface and override the compare() method. Here I will detail the use of the Comparable interface and the compareTo() method.
Codes:
1 | package discussionassignment; |
Outputs:
1 | 0 |
In the above code, the Student class implements the Comparable interface and overrides the CompareTo method. If student’s student number is bigger than the student2’s student number, the output of student1.compareTo(student2) will be a positive number; If student’s student number is less than the student2’s student number, the output of student1.compareTo(student2) will be a negative number; If student’s student number is equal to the student2’s student number, the output of student1.compareTo(student2) will be zero.
Furthermore, because the student numbers of Student1 and Student2 are both 0001, the result of Student1.compareTo (Student2) is 0. Unlike the “false“ returned by the statement Student1.equals(Student2), the result “0“ is in line with our expectations. It is what we want.
Word count: 849
References
1.Eck, D. J. (2019). Introduction to Programming Using Java.
http://math.hws.edu/javanotes
2.Oracle. (n.d.). equals().
https://docs.oracle.com/javase/8/docs/api/