Defining Equality
Python |
Java |
The == operator compares two objects for equality. This operator uses the is operator by default.
The is operator tests two object references for
equality: do they refer to the exact same object in memory? Often, this test is too
restrictive. A more relaxed
version would compare one or more of the objectsÕ attributes for
equality. For example, two Student objects could have the same name and be
considered equal, even though they are also distinct objects. This type of equality is called
structural equivalence, as opposed to the more restrictive type of object
identity. The programmer can
override the default definition of == by including a definition of the method __eq__ in a given class.
There are actually three tests to perform. The receiver object (self) and the second parameter object (other) are first compared for identity using the is operator. If this test
fails, the types of the two objects are then compared. If this test succeeds, their relevant
attributes are compared using the == operator.
The behavior of != can also be modified by overriding the method __ne__. Example: class Student: NUM_GRADES = 5 def __init__(self, name):
self.name = name
self.grades = [] for
i in range(Student.NUM_GRADES):
self.grades.append(0) def __eq__(self, other):
"""
Compare the names for equality if the
objects are not identical.
"""
if self is other:
return True
elif type(self) != type(other):
return False
else:
return self.name == other.name def __ne__(self, other):
"""
Compare the names for inequality.
"""
return not self == other Usage: s1 =
Student('Mary') s2 = Student('Bill') s3 = Student('Bill') print(s1 ==
s2) #
displays False
print(s2 ==
s3) #
displays True
print(s2 is
s3) #
displays False
print(s2 !=
s3) #
displays False
|
The equals() method
compares two objects for
equality. This method uses the == operator by default.
The == operator tests two object references for
equality: do they refer to the exact same object in memory? Often, this test is too
restrictive. A more relaxed version
would compare one or more of the objectsÕ attributes for equality. For example, two Student objects could have the same name and be
considered equal, even though they are also distinct objects. This type of equality is called
structural equivalence, as opposed to the more restrictive type of object
identity. The programmer can
override the default definition of equals by including a definition of this method in a
given class. The attributes
being compared are those of the receiver object (this) and the parameter object (other). The method header for equals is public boolean
equals(Object other) Note that the parameter
objectÕs type is Object.
This allows any object to be compared with the receiver object for
equality. Consequently, there
are actually three tests to perform in equals.
The first one compares the two objects for identity using ==. The second one uses
the instanceof operator to determine if the type of the
parameter object is the same as that of the receiver object. The third test compares the relevant
attributes of the two objects using the equals method with them. Before accessing the
parameterÕs attributes, the type of the parameter must be cast down to the
receiver objectÕs type using a cast operator. Example: public class
Student{ public static final int
NUM_GRADES = 5; private String name; private int[] grades; public Student(String
name){
this.name = name;
this.grades = new int[NUM_GRADES]; } public boolean
equals(Object other){
/*
Compare the names for equality if the
objects are not identical.
*/
if (this == other)
return true;
else if !(other instanceof Student)
return false;
else{
Student otherStudent = (Student)other;
return this.name.equals(otherStudent.name); } } } Usage: Student s1 =
new Student("Mary"); Student s2 =
new Student("Bill"); Student s3 =
new Student("Bill"); System.out.println(s1.equals(s2));
// displays false System.out.println(s2.equals(s3));
// displays true System.out.println(s1
== s3);
// displays false System.out.println(!
s2.equals(s3));
// displays false |