Difference between == and equals() in java.
Example:- == check object reference are same or not.It check only the objects are same, not equality of object.
- equals() check reference values are same are not.Its checks the equality of the object fields. If you want to check the object equality we need to override the equals().
//Value stored in String Pool
String l_sValue = "Equals";
//Reference stored in heap memory.
String l_sSecValue = new String("Equals");
//Both objects having same value. The values are equals.So its returns true.
if(l.equals(k)) Output: return true;
//Both are addressing different object, returns false.
if((l == k)) Output: return false;
/**
* @author palrajb
*
*/
public class Test {
private String name;
private int age;
private int marks;
public Test() {
}
public Test(String name, int age, int marks) {
this.name = name;
this.age = age;
this.marks = marks;
}
public void setMarks(int marks) {
this.marks = marks;
}
/**
* @param args
*/
public static void main(String[] args) {
Test t = new Test("1", 1, 1);// Create the object t with three parameter constructor.
Test t1 = (Test) t;// Addressing same reference.
t1.setMarks(2); // Changes on t1 directly affect t. Because t1 is addressing t reference.
// Here t and new object having same values, but we are not override the equals() to equals the fields of Test class. So passing reference are different.
System.out.println(t.equals(new Test("1", 1, 1)));Output : false
// Here we are t and t1 are addressing single object t.So their address and fields are same, returns true.
System.out.println(t.equals(t1)); Output : true
// Here we are t and t1 are addressing single object t. As per state check only the objects are same, return true.
System.out.println(t == t1);Output : true
}
if you want to make t.equals(new Test("1",1,1)) true, we need to override the equals()
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Test other = (Test) obj;
if (age != other.age)//Here checking the age of t and new Test("1",1,1) object age is not same.
return false;
if (name == null) {//Null check for name, because we have setter method for name variable.
if (other.name != null)//Same null check for new Test("1",1,1).
return false;
} else if (!name.equals(other.name))//Here checking the name of t and new Test("1",1,1) object name is not same.
return false;
return true;
}
After the equals() implementation, now execute the
System.out.println(t.equals(new Test("1", 1, 1)));//Output: true. Because we customized the equals method as we want. We just compare the age and name variable only, we are not consider the marks variable for equals() implementation so changes the value by using
t1.setMarks(2); not affect anything on the equality.
Meanwhile we know that if we override the equals() then we need to override hashCode().
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + age;
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
Do you feel we would missed any variable in equals() and hashCode().
Yes, the marks variable is missing. Key to override the equals() and hashCode() is the filed used in equals() must be used hashCode implementation.