What is the difference between == vs equals() in Java? -
i wanted clarify if understand correctly:
==
-> reference comparison, i.e. both objects point same memory location.equals()
-> evaluates comparison of values in objects
am correct in understanding ?
in general, answer question "yes", but...
- equals compare written compare, no more, no less.
- if class not override equals method, defaults
equals(object o)
method of closest parent class has overridden method. - if no parent classes have provided override, defaults method ultimate parent class, object, , you're left
object#equals(object o)
method. per object api same==
; is, returns true if , if both variables refer same object, if references 1 , same. testing object equality , not functional equality. - always remember override
hashcode
if overrideequals
not "break contract". per api, result returnedhashcode()
method 2 objects must same ifequals
methods shows equivalent. converse not true.
Comments
Post a Comment