ZetCode

Java Objects

last modified August 28, 2026

In this article we show how to use the java.util.Objects utility class.

Objects is a final class that consists of static utility methods for operating on objects. These methods include null-safe methods for computing the hash code of an object, comparing two objects, or returning a string for an object. The class also provides requireNonNull methods for checking that an object reference is not null.

The methods of Objects are useful because they help us avoid NullPointerException. For instance, Objects.equals accepts two potentially null references, while calling equals directly on a null reference throws an exception.

Objects methods

The following table provides a few Objects methods.

Modifier and type Method Description
static boolean equals(Object a, Object b) Returns true if the arguments are equal to each other and false otherwise.
static boolean deepEquals(Object a, Object b) Returns true if the arguments are deeply equal to each other.
static int hashCode(Object o) Returns the hash code of a non-null argument.
static int hash(Object... values) Generates a hash code for a sequence of input values.
static <T> int compare(T a, T b, Comparator<? super T> c) Returns 0 if the arguments are identical, otherwise c.compare(a, b).
static String toString(Object o) Returns the result of calling toString on a non-null argument.
static String toString(Object o, String nullDefault) Returns the result of calling toString on a non-null argument, or nullDefault if the argument is null.
static <T> T requireNonNull(T obj) Checks that the specified object reference is not null.
static boolean isNull(Object obj) Returns true if the provided reference is null.
static boolean nonNull(Object obj) Returns true if the provided reference is non-null.

In this article we work with several of these methods.

Objects.equals

The equals method returns true if the two arguments are equal to each other. It is null-safe: if both arguments are null, it returns true.

Main.java
import java.util.Objects;

void main() {

    String a = "sky";
    String b = "sky";
    String c = null;

    System.out.println(Objects.equals(a, b));
    System.out.println(Objects.equals(a, c));
    System.out.println(Objects.equals(c, null));
}

The example compares a pair of strings and a null reference.

System.out.println(Objects.equals(c, null));

When both arguments are null, Objects.equals returns true.

true
false
true

Objects.deepEquals

The deepEquals method is designed for arrays. It returns true if the arguments are deeply equal; that is, if they are arrays, it compares their contents element by element.

Main.java
import java.util.Objects;

void main() {

    int[] nums1 = {1, 2, 3};
    int[] nums2 = {1, 2, 3};

    System.out.println(Objects.equals(nums1, nums2));
    System.out.println(Objects.deepEquals(nums1, nums2));
}

The example compares two arrays that contain the same values.

System.out.println(Objects.equals(nums1, nums2));

The equals method compares array references, so it returns false here.

System.out.println(Objects.deepEquals(nums1, nums2));

The deepEquals method compares the contents of the arrays.

false
true

Objects.hash

The hash method generates a hash code for a sequence of values. It is often used in hashCode implementations of custom classes.

Main.java
import java.util.Objects;

void main() {

    String name = "John Doe";
    int age = 34;

    int hash1 = Objects.hash(name, age);
    int hash2 = Objects.hash(name, age);

    System.out.println(hash1);
    System.out.println(hash1 == hash2);
}

The example computes a hash code from two values.

int hash1 = Objects.hash(name, age);

The hash method combines the hash codes of the given values into a single hash code.

-2041614833
true

Equal sequences of values produce equal hash codes.

Objects.compare

The compare method compares two objects with a given comparator. If the arguments are identical, it returns 0 without invoking the comparator.

Main.java
import java.util.Comparator;
import java.util.Objects;

void main() {

    Integer a = 3;
    Integer b = 5;

    System.out.println(Objects.compare(a, b, Comparator.naturalOrder()));
    System.out.println(Objects.compare(null, b, Comparator.naturalOrder()));
    System.out.println(Objects.compare(a, null, Comparator.naturalOrder()));
}

The example compares integer values with the natural ordering.

System.out.println(Objects.compare(null, b, Comparator.naturalOrder()));

A null reference is considered smaller than a non-null reference; the comparator is not invoked.

-1
-1
1

Objects.toString

The toString method returns the string representation of an object. The overloaded version takes a default value that is used when the argument is null.

Main.java
import java.util.Objects;

void main() {

    String msg = "Hello";
    String nullMsg = null;

    System.out.println(Objects.toString(msg));
    System.out.println(Objects.toString(nullMsg));
    System.out.println(Objects.toString(nullMsg, "no message"));
}

The example prints the string representation of two values.

System.out.println(Objects.toString(nullMsg));

For a null argument, the method returns the string "null".

System.out.println(Objects.toString(nullMsg, "no message"));

The second overload returns the default value instead.

Hello
null
no message

Objects.requireNonNull

The requireNonNull method checks that the given reference is not null. If it is null, a NullPointerException is thrown. The method is commonly used in constructors to fail fast on invalid arguments.

Main.java
import java.util.Objects;

class User {

    private final String name;

    User(String name) {

        this.name = Objects.requireNonNull(name, "name must not be null");
    }

    String getName() {

        return name;
    }
}

void main() {

    var user = new User("John Doe");
    System.out.println(user.getName());

    try {

        new User(null);

    } catch (NullPointerException e) {

        System.out.println(e.getMessage());
    }
}

The example passes a null value to the User constructor.

this.name = Objects.requireNonNull(name, "name must not be null");

If the name is null, the constructor throws a NullPointerException with the given message.

John Doe
name must not be null

Objects.isNull and nonNull

The isNull and nonNull methods are predicates for null checks. They are often used as method references in the Stream API.

Main.java
import java.util.List;
import java.util.Objects;

void main() {

    var words = List.of("sky", null, "cloud", null, "ocean");

    long nullCount = words.stream()
            .filter(Objects::isNull)
            .count();

    var nonNullWords = words.stream()
            .filter(Objects::nonNull)
            .toList();

    System.out.printf("null count: %d%n", nullCount);
    System.out.println(nonNullWords);
}

The example counts null values and collects non-null values from a list.

.filter(Objects::isNull)

The isNull method is used as a filter predicate.

null count: 2
[sky, cloud, ocean]

Source

Java Objects - language reference

In this article we have presented the java.util.Objects utility class. We have covered null-safe equality, hashing, comparison, string conversion, and null checks.

Author

My name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming.

List all Java tutorials.