2.1. Annotations2. Usage2.3. Floating point
PreviousUpNext

Complaints
Handling of equality in Java is usually considered to be one of the worst design mistakes of the language. The problems are manifold. Firstly, the equals method is defined on the java.lang.Object type from which all Java classes are forced to inherit. This means that equality must be defined for all types, even those that do not have sensible equality relations [1]. If an object forgets to override equals, then the object is given reference equality by default. Secondly, the argument to the equals method is of type java.lang.Object, meaning that it's perfectly possible to query the equality of values of different types (which almost never makes logical sense). Thirdly, the equals method must be consistent with the associated hashCode method in order for the standard library collections to behave correctly.
Had Java been designed today, the correct way to design equals, assuming that java.lang.Object still existed, would be for java.lang.Object to declare neither equals nor hashCode. A static method same would be defined in java.lang.Object in order to compare the addresses of two values. The following interface types would be provided by the standard library:
public interface Equals<A>
{
  boolean equals(A x);
}

public interface Hashable<A extends Equals<A>>
{
  int hashCode();
}
Effectively, equality is only defined for objects that explicitly implement Equals. The Equals type implies structural equality. Objects can only be placed into collections that require hashing if they explicitly implement Hashable [2]. The invariance of generic parameters means that comparing objects that are of different types is statically prevented.
This is the approach taken by far safer languages such as Haskell. Unfortunately, it is too late to fix these major design flaws in Java.

[1]
Consider the required equality relation for function types.
[2]
The constraint on the generic parameter of Hashable is intended to imply that all Hashable objects also have a defined equality relation. This could arguably be removed.

PreviousUpNext
2.1. Annotations2. Usage2.3. Floating point