Answer: Both
equals() and "==" operator in Java is used to compare objects to
check equality but main difference between equals method and == operator
is that former is method and later is operator. Since Java doesn’t support operator overloading, == behaves identical for every object but equals() is method, which can
be overridden in Java and logic to compare objects can be changed based upon
business rules.
Another difference between == and equals
method is that former is used to compare both primitive and objects while later
is only used for objects comparison.
Another difference is ‘==’ operator compare reference or memory location of
objects in heap, whether they point to same location or not
.whenever we create any object using the operator new it will create new
memory location for that object. So we use == operator to check memory location
or address of two objects are same or not. And when we talk about ‘equals() method’ main purpose is
two compare the state of two objects or contents of the object.
0 comments :
Post a Comment