CS1103 Discussion Assignment Unit 4 - Advantages and disadvantages of using equals() and the alternative for equality

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
2
3
4
5
6
7
8
9
10
11
equals():
public boolean equals(Object obj)
Indicates whether some other object is "equal to" this one.


Parameters:
obj - the reference object with which to compare.


Returns:
true if this object is the same as the obj argument; false otherwise.
equals() in the Object class (Oracle, n.d.)
In the Object class, the equals() method tests whether two objects have the same reference (the address in memory). However, in the subclass of Object, sometimes we need to compare the object's content instead of its references. In this case, we need to override the equals() methods in the subclass (Eck, 2019).

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
2
3
4
5
6
7
8
9
10
11
12
13
package discussionassignment;

public class TestEquality {
public static void main(String[] args) {
// the "hello" is stored in the constant pool
String str1 = "hello";
// the "hello" is stored in the heap
String str2 = new String("hello");

System.out.println(str1 == str2); // false
System.out.println(str1.equals(str2)); // true
}
}

Outputs:

1
2
3
4
false
true

Process finished with exit code 0

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
2
3
4
5
6
7
8
9
10
11
12
13
14
public class Student {
String studentNum = "";

public Student(String studentNum) {
this.studentNum = studentNum;
}

public static void main(String[] args) {
Student student1 = new Student("0001");
Student student2 = new Student("0001");
// if the student numbers are the same, s1 and s2 are the same student
System.out.println(student1.equals(student2)); // false
}
}

Outputs:

1
2
3
false

Process finished with exit code 0

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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package discussionassignment;

public class Student implements Comparable<Student> {
String studentNum = "";

public Student(String studentNum) {
this.studentNum = studentNum;
}

@Override
public int compareTo(Student o) {
// return 0 if the student numbers are the same.
return this.studentNum.compareTo(o.studentNum);
}

public static void main(String[] args) {
Student student1 = new Student("0001");
Student student2 = new Student("0001");
System.out.println(student1.compareTo(student2)); // 0
}
}

Outputs:

1
2
3
0

Process finished with exit code 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/

​


This is the ending, thanks for reading.