This is probably a bad idea for the reasons already outlined in the other answers.
Regarding the “legal” aspects, Contract of Object.equals claims
equals :
- : , x.equals() .
- : x y x.equals(y) true , y.equals(x) true.
- : x, y z, x.equals(y) true, y.equals(z) true, x.equals(z) true.
- : x y x.equals(y) true false, , , .
- , x.equals(NULL) .
:
- reflexive: , -
this == o
- : -
instanceof
- . - : ,
- :
x.equals(null)
false: , - instanceof
, , , - . getClass() over instanceof .equals()?.
, , hashCode
, .
:
x
y
class Point {
final int x;
final int y
public Point(int x, int y) {
this.x = x;
this.y = y;
}
}
- > 2^32 * 2^32 = 2^64
, 2^32
-. , , equals
.
. equals hashCode: Objects.hash? -, - , Objects.hash
Strings
.