Reputation: 117
class Employee{
private String name;
private int rollno;
public Employee(String name,int rollno)
{
this.name=name;
this.rollno=rollno;
}
}
public class CT2{
public static void main(String[]args){
Employee emp1 = new Employee("Raghu",35);
Employee emp2 = new Employee("Raghu",35);
Employee emp3 = new Employee("Raghu",35);
if(emp2.equals(emp3))
System.out.println("They are equal");
else
System.out.println("They are not equal");
}
}
What is wrong with the above code?? Ideally it should print "They are equal" but i am getting output as "They are not equal"
Upvotes: 7
Views: 3893
Reputation: 122026
Inorder to make that work if(emp2.equals(emp3))
you need to override the default behaviour of equals method in your Employee class.
@Override
public boolean equals(Object other){
/// your implementation of equals ..
}
Form Object class equals method docs
Note that it is generally necessary to override the
hashCode()
method whenever this method is overridden, so as to maintain the general contract for thehashCode()
method, which states that equal objects must have equal hash codes.
Upvotes: 3
Reputation: 823
Put this in the employee class.
public boolean equals(Object object){
if(object instanceof Employee){
Employee emp2= (Employee) object;
if(this.name.equals(emp2.name) && this.rollno == emp2.rollno))
return true;
return false;
}
return false;
}
Upvotes: 0
Reputation: 1011
As per the java doc,The equals method for class Object implements the most discriminating possible equivalence relation on objects; that is, for any non-null reference values x and y, this method returns true if and only if x and y refer to the same object (x == y has the value true).
/* Object.equals() */
public boolean equals(Object obj) {
return (this == obj);
equals() for Object compares references. That is why it is false since they are different Objects. equals() designed to use internal state of the objects for comparison.
To solve your problem equals method needs to be overridden inside the class Employee.
Upvotes: 0
Reputation: 86
Currently Employee object emp2
uses Object class's equals()
method to compare the two object references. Below snippet shows Object class equals()
method,
public boolean equals(Object obj) {
return (this == obj);
}
You need to override this equals()
method to make two different objects equal.
Upvotes: 0
Reputation: 8663
You need to implement equals
method in your class.
The equals method for class Object implements the most discriminating possible equivalence relation on objects; that is, for any non-null reference values x and y, this method returns true if and only if x and y refer to the same object (x == y has the value true).
Adding below method will work for you.
@Override
public boolean equals(Object o) {
// If the object is compared with itself then return true
if (o == this) {
return true;
}
/* Check if o is an instance of Complex or not
"null instanceof [type]" also returns false */
if (!(o instanceof Employee)) {
return false;
}
// typecast o to Complex so that we can compare data members
Employee c = (Employee) o;
// Compare the data members and return accordingly
return (rollno == c.rollno && Objects.equals(name, c.name))
}
Upvotes: 7
Reputation: 13858
If you do not write your own equals()
, Java will use the default - comparing object identity.
As you created two objects, this will fail.
As creator of the class only you can tell, when objects of these class should be considered equal even if they are separate instances.
You probably need something like
public boolean equals(Object o) {
if(o instanceof Employee) {
Employee other = (Employee)o;
return this.rollno == other.rollno && this.name.equals(other.name);
}
return false;
}
Remember that whenever you implement equals
you should consider implementing hashCode
as well.
Upvotes: 0
Reputation: 9437
Since you don't override the toString method yourself, it actually uses the version of Object.
This is actually:
public boolean equals(Object o){
return this == o;
}
In your example, your objects do have the same value, but not the same reference, hence, it fails.
Add:
@Override
public boolean equals(Object o){
if ( !(o instanceof(Employee)){
return false;
}
Employee t = (Employee)o;
return this.name.equals(t.name) && this.rollno == t.rollno;
}
In order to be completely align: also override the hashCode method.
Do understand that it is not impossible for the equals method I've written above to throw an NullPointerException, since you can pass null for the name to the constructor.
Upvotes: 0