CKTIS / Java
Object.equals — object equality
Object.equals compares object identity by default. Overriding it lets a class define value equality.
Version: Java SE 21; repository runtime, production runtime not attested
Syntax
public boolean equals(Object obj)Object.equals(Object obj) returns boolean. The implementation in Object tests identity: for non-null references it behaves like ==, without automatically comparing fields. Classes may override it.
The contract requires reflexivity, symmetry, transitivity, and consistent results while comparison data remains unchanged. For non-null x, x.equals(null) must return false.
Overriding equals generally requires overriding hashCode: equal objects must have equal hashes; equal hashes do not imply equality.
The example demonstrates inherited behavior: the same instance, another instance, and a null argument.
Examples
public class Main {
public static void main(String[] args) {
Object a = new Object();
Object b = new Object();
Object same = a;
System.out.println(a.equals(same));
System.out.println(a.equals(b));
System.out.println(a.equals(null));
}
}Result
true false false
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.