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 override equals not "break contract". per api, result returned hashcode() method 2 objects must same if equals methods shows equivalent. converse not true.

Comments

Popular posts from this blog

php - How to add and update images or image url in Volusion using Volusion API -

javascript - IE9 error '$'is not defined -