Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Java

Overriding Object.equals: comparing both Person fields

Define Person equality by name and age, distinguish it from reference identity, and implement a matching hashCode method.

Version: Java SE 21

Syntax

@Override public boolean equals(Object obj)
@Override public int hashCode()

By default, Object.equals(Object) checks identity: for two non-null references, it returns true only if they refer to the same object. Reference == also checks identity, not field values. In this example, two Person objects are equal when both name and age match, even if they are separate instances.

An Object.equals override uses public boolean equals(Object obj). Add @Override so the compiler checks that the method overrides an inherited method. equals(Person) would be an overload and would not replace equals(Object). After the quick this == obj check, the instanceof Person other pattern rejects null and objects of other types. Person is final, so there are no subclasses. Compare the int age with == and the name with Objects.equals, which treats two null values as equal.

For non-null objects, equality must be reflexive, symmetric, and transitive. Repeated comparisons must agree while the comparison data stays unchanged, and x.equals(null) must return false when x is non-null. Override hashCode() using the same two fields: equal objects must have the same hash code, but unequal objects can share one too.

Examples

import java.util.Objects;

public class Main {
    private static final class Person {
        private final String name;
        private final int age;

        Person(String name, int age) {
            this.name = name;
            this.age = age;
        }

        @Override
        public boolean equals(Object obj) {
            if (this == obj) {
                return true;
            }
            if (!(obj instanceof Person other)) {
                return false;
            }
            return age == other.age && Objects.equals(name, other.name);
        }

        @Override
        public int hashCode() {
            return Objects.hash(name, age);
        }
    }

    public static void main(String[] args) {
        Person a = new Person("Ada", 28);
        Object b = new Person("Ada", 28);
        System.out.println(a == b);
        System.out.println(b.equals(a));
        System.out.println(a.equals(new Person("Ada", 29)));
        System.out.println(a.equals(new Person("Eva", 28)));
        System.out.println(a.equals(null));
        System.out.println(a.equals("Ada"));
        System.out.println(a.hashCode() == b.hashCode());
    }
}

Result

false
true
false
false
false
false
true

The two distinct instances fail `==` but pass `equals`, even though `b` is declared as `Object`. Changing either field, passing `null`, or passing a different type gives `false`. The equal instances have matching hash codes.

Related lessons

  • Java · Lesson 28 · Intermediate · Version: 2

Sources