How default .equals and .hashCode will work for my classes?

前端 未结 6 1717
不思量自难忘°
不思量自难忘° 2020-11-28 23:54

Say I have my own class

public class MyObj { /* ... */ }

It has some attributes and methods. It DOES NOT implement equals, DOES NOT impleme

6条回答
  •  臣服心动
    2020-11-29 00:44

    Yes, the default implementation is Object's (generally speaking; if you inherit from a class that redefined equals and/or hashCode, then you'll use that implementation instead).

    From the documentation:

    equals

    The equals method for class Object implements the most discriminating possible equivalence relation on objects; that is, for any non-null reference values x and y, this method returns true if and only if x and y refer to the same object (x == y has the value true).

    hashCode

    As much as is reasonably practical, the hashCode method defined by class Object does return distinct integers for distinct objects. (This is typically implemented by converting the internal address of the object into an integer, but this implementation technique is not required by the JavaTM programming language.)

提交回复
热议问题