Difference Between == and .equals() Method in Java
Jan 7, 2022
Description:
“==” checks if both objects point to the same memory location whereas . equals() evaluates to the comparison of values in the objects.
Case:1
When we initialize the string with “new String” memory location will allocate.
In below case Value (“test1”) Same for both the object but two different memory location was allocated. When we compare with “==” its comparing the memory location whether same or not.
Result:
CASE 2:
In below case both the objects into same memory location and so its value equal.
EQUALS Case:
In below case “equals” comparing the value only .Here both object values are same.