When do you override hashCode() and equals()?
hashCode()
and equals()
methods have been defined in Object
class which is parent class for java objects. For this reason, all java objects inherit a default implementation of these methods.hashCode()
method is used to get a unique integer for given object. This integer is used for determining the bucket location, when this object needs to be stored in some HashTable
like data structure. By default, Object’s hashCode()
method returns and integer representation of memory address where object is stored.equals()
method, as name suggest, is used to simply verify the equality of two objects. Default implementation simply check the object references of two objects to verify their equality.
Note that it is generally necessary to override the hashCode method whenever this method is overridden, so as to maintain the general contract for the
hashCode()
method, which states that equal objects must have equal hash codes.equals()
must define an equality relation (it must be reflexive, symmetric and transitive). In addition, it must be consistent (if the objects are not modified, then it must keep returning the same value). Furthermore,o.equals(null)
must always returnfalse
.hashCode()
must also be consistent (if the object is not modified in terms ofequals()
, it must keep returning the same value).
The relation between the two methods is:
Whenever a.equals(b)
then a.hashCode()
must be same as b.hashCode()
.
What is the difference between equals() and == ?
Equals() method is defined in Object class in Java and used for checking equality of two objects defined by business logic.“==” or equality operator in Java is a binary operator provided by Java programming language and used to compare primitives and objects. public boolean equals(Object o) is the method provided by the Object class. The default implementation uses == operator to compare two objects. For example: method can be overridden like String class. equals() method is used to compare the values of two objects.
Q) What is the difference between equals() and == ?
Ans) == operator is used to compare the references of the objects.
public boolean equals(Object o) is the method provided by the Object class. The default implementation uses == operator to compare two objects which mean by default the objects address are compared. But since the method can be overriden like for String class. equals() method can be used to compare the values of two objects.
String str1 = "MyName";
String str2 = "MyName";
String str3 = new String(str2);
if (str1 == str2) {
System.out.println("Objects are equal")
}else{
System.out.println("Objects are not equal")
}
if(str1.equals(str2)) {
System.out.println("Objects are equal")
} else {
System.out.println("Objects are not equal")
}
Output:
Objects are not equal
Objects are equal
String str2 = "MyName";
String str3 = str2;
if (str2 == str3) {
System.out.println("Objects are equal")
}else{
System.out.println("Objects are not equal")
}
if (str3.equals(str2)) {
System.out.println("Objects are equal")
} else {
System.out.println("Objects are not equal")
}
Output:
Objects are equal
Objects are equal
public boolean equals(Object o) is the method provided by the Object class. The default implementation uses == operator to compare two objects which mean by default the objects address are compared. But since the method can be overriden like for String class. equals() method can be used to compare the values of two objects.
String str1 = "MyName";
String str2 = "MyName";
String str3 = new String(str2);
if (str1 == str2) {
System.out.println("Objects are equal")
}else{
System.out.println("Objects are not equal")
}
if(str1.equals(str2)) {
System.out.println("Objects are equal")
} else {
System.out.println("Objects are not equal")
}
Output:
Objects are not equal
Objects are equal
String str2 = "MyName";
String str3 = str2;
if (str2 == str3) {
System.out.println("Objects are equal")
}else{
System.out.println("Objects are not equal")
}
if (str3.equals(str2)) {
System.out.println("Objects are equal")
} else {
System.out.println("Objects are not equal")
}
Output:
Objects are equal
Objects are equal
https://www.youtube.com/watch?v=ghswNpRv2t0
String comparison with equals() and ‘==’
Another favorite area in interviews. There are generally two ways to compare objects
- Using == operator
- Using equals() method
==
operator compare for object references i.e. memory address equality. So if two string objects are referring to same literal in string pool or same string object in heap then s==t
will return true
, else false
.
equals()
method is overridden in String class and it verify the char sequences hold by string objects. If they store the same char sequence, the s.equals(t) will return true, else false.
==
operator compare for object references i.e. memory address equality. So if two string objects are referring to same literal in string pool or same string object in heap then s==t
will return true
, else false
.equals()
method is overridden in String class and it verify the char sequences hold by string objects. If they store the same char sequence, the s.equals(t) will return true, else false.
No comments:
Post a Comment