...
As a general rule, use the Object.equals()
method to check whether two objects are abstractly equal to each other. Reserve use of the equality operators ==
and !=
for testing whether two references specifically refer to the same object (this is reference equality). See also guideline MET13-J. Ensure Classes that hashCodedefine an equals() is overridden when equalsmethod must also define a hashCode() is overriddenmethod.
When operating on numeric boxed types (for example,Byte
, Character
, Short
, Integer
, Long
, Float
, and Double
), the numeric relational operators (such as <
, <=
, >
, and >=
) produce results that match those provided for arguments of the equivalent primitive numeric types. Specifically, the JLS requires auto-unboxing in this case, which results in comparison of the numeric values contained in the boxed objects. (See JLS Section 5.6.2, "Binary Numeric Promotion".) But when both arguments of an equality operator (for example, ==
or !=
) are of a numeric boxed type, the operation is a reference comparison rather than the anticipated numeric comparison, which can produce unexpected results. (See guideline EXP03-J. Avoid the equal and not equal operators when comparing values of boxed primitives.)
...