Reputation: 177
I want to ask a question about using the equals method to test object equality in Java.
I am a beginner in Java and currently progressing through the Dummies Java 9-in-1 book.
I have written the following code to check the equality of two Employee
objects:
public class TestEquality2 {
public static void main (String [] args) {
Employee emp1 = new Employee ("Martinez", "Anthony");
Employee emp2 = new Employee ("Martinez", "Anthony");
if (emp1.equals(emp2))
System.out.println("These employees are the same");
else
System.out.println("These employees are different.");
}
}
class Employee {
private String firstName;
private String lastName;
public Employee (String firstName, String lastName) {
this.lastName = lastName;
this.firstName = firstName;
}
public String getLastName() {
return this.lastName;
}
public String getFirstName() {
return this.firstName;
}
public boolean equals (Object obj) {
// an object must equal itself
if (this == obj)
return true;
// no object equals null
if (this == null)
return false;
if (this.getClass() != obj.getClass())
return false;
// Cast to an employee, then compare the fields
Employee emp = (Employee) obj;
// is this the string's equals method?
return this.lastName.equals(emp.getLastName()) && this.firstName.equals(emp.getFirstName());
}
}
The line of concern is the last one of the equals(Object obj)
method.
As per the code, I have overriden the default Object equals ()
method and supplied my own design, but I was confused here:
return this.lastName.equals(emp.getLastName()) && this.firstName.equals(emp.getFirstName());
I know that lastName
is a string, but the equals()
method that I am using here, is this the equals()
method for a String
or the one I've just defined? If it's the latter, I know that I would create a recursive situation, although I'm confident I am using the String equals()
yet I want to clarify for completion.
Upvotes: 3
Views: 1805
Reputation: 1133
Basically, you override the equals() method because you want to have two different objects in memory to be considered equal in your application, like in your case you created two different Employee class objects as below :
Employee emp1 = new Employee ("Martinez", "Anthony");
Employee emp2 = new Employee ("Martinez", "Anthony");
These objects are actually two different objects in memory or from a real-life perspective, they are two different individuals and If you will not override equals() , it will check equals() from Object class which has the following implementation :
public boolean equals(Object obj) {
return (this == obj);
}
and emp1.equals(emp2) will return false.
But might be your application logic is such that you want these two Individuals to be considered equal(same), so in that case, you will override the equals() method.
While overriding equals, you can choose on what basis you want two Individuals(employees or objects) to be equal, like in your case you have chosen firstName and lastName(you could have chosen the only lastName as well, depending upon your requirement).
So in the following line, basically you are saying that if two employees firstName and surName are equal then consider them equal,even when you know that they are two different individuals(objects in memory) :
return this.lastName.equals(emp.getLastName()) && this.firstName.equals(emp.getFirstName());
And for that you are calling equals() of String class on lastName and firstName which are Strings.String class has already overridden equals() method by Java library developers like this:
public boolean equals(Object anObject) {
if (this == anObject) {
return true;
}
if (anObject instanceof String) {
String aString = (String)anObject;
if (coder() == aString.coder()) {
return isLatin1() ? StringLatin1.equals(value, aString.value)
: StringUTF16.equals(value, aString.value);
}
}
return false;
}
Hope this was helpful.
Upvotes: 0
Reputation: 312219
lastName
is a String
, so calling this.lastName.equals(emp.getLastName())
will use String
's implementation of the equals
method. The same goes for the comparison of the first names, of course.
Upvotes: 3