Java (equality method)

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 code above? Ideally, he should print “They are equal,” but I get the output as “They are not equal”

+4
source share
8 answers

You need to implement a method equalsin your class.

The equals method for the Object class implements the most varied possible equivalence relation for objects; that is, for any non-empty reference values ​​x and y, this method returns true if and only if x and y refer to the same object (x == y is 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))
}
+5
source

if(emp2.equals(emp3)), equals Employee.

@Override
public boolean equals(Object other){
    /// your implementation of equals ..
}

equals method docs

, hashCode() , , hashCode(), , -.

+3

equals Employee.

0

equals(), Java .

, .

, , , .

, , -

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;
}

, , equals, hashCode.

0

toString , Object.

:

public boolean equals(Object o){
  return this == o;
}

, , , .

@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;
}

: hashCode.

, equals, , , NullPointerException, null .

0

.

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;
}
0

Employee emp2 Object class equals() . equals(),

 public boolean equals(Object obj) {
        return (this == obj);
 }

equals(), .

0

java- equals Object ; x y true , x y (x == y true).

/* Object.equals() */
public boolean equals(Object obj) {
    return (this == obj);

equals () for an object compares references. That is why this is not true, since they are different Objects. equals (), designed to use the internal state of objects for comparison.

To solve your problem, the equals method must be redefined inside the Employee class.

0
source

Source: https://habr.com/ru/post/1621242/


All Articles