hashCode and equals and ==

hashCode () and equals () the relevant provisions:

  1. If two objects are equal, it must also be the same hashcode
  2. Two objects are equal, returns true equals two methods
  3. Hashcode two objects have the same value, they are not necessarily equal
  4. In summary, equals overridden the method, the method must be covered hashCode
  5. The default behavior hashCode () is to create a unique value to objects on the heap. If no override hashCode (), then the two objects are not equal class anyway (even if these two objects point to the same data).

== and equals the difference

  1. == two variables are determined or examples are not directed to the same memory space equals the value of two variables are determined or examples points to the memory space is not the same
  2. == refers to the memory address comparing equals () is compared to the contents of the string
  3. Are the same references refer == equals () refers to the values ​​are identical
Published 91 original articles · won praise 32 · views 30000 +

Guess you like

Origin blog.csdn.net/qq_41345773/article/details/104990531