Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

Java Dictionary

  1. Home
  2. Java Dictionary
  3. Objects.equals() / Objects.toString() / Objects.requireNonNull()

Objects.equals() / Objects.toString() / Objects.requireNonNull()

The java.util.Objects class provides utility methods for working with null values safely. It lets you write concise null-safe comparisons, string conversions, and validations (available since Java 7).

Syntax

// Performs a null-safe equality comparison.
boolean result = Objects.equals(Object a, Object b);

// Converts an object to a string, safely handling null.
String str = Objects.toString(Object o);
String str = Objects.toString(Object o, String nullDefault);

// Throws NullPointerException if the argument is null.
T value = Objects.requireNonNull(T obj);
T value = Objects.requireNonNull(T obj, String message);

// Checks whether a value is null or non-null.
boolean isNull   = Objects.isNull(Object obj);
boolean nonNull  = Objects.nonNull(Object obj);

// Computes a null-safe hash code.
int hash = Objects.hashCode(Object o);
int hash = Objects.hash(Object... values);

Method List

MethodDescription
Objects.equals(a, b)Compares two values safely with null support. Returns true if both are null, false if only one is null.
Objects.toString(o)Returns the string "null" if o is null.
Objects.toString(o, nullDefault)Returns nullDefault if o is null.
Objects.requireNonNull(obj)Throws NullPointerException if obj is null. Use this for precondition checks on arguments.
Objects.requireNonNull(obj, message)Same as above, but lets you specify a custom exception message.
Objects.isNull(obj)Equivalent to obj == null. Useful as a method reference (Objects::isNull).
Objects.nonNull(obj)Equivalent to obj != null.
Objects.hash(values...)Generates a hash code from multiple values. Useful for implementing hashCode().

Sample Code

import java.util.Objects;

// Use equals() to compare values safely with null.
String a = null;
String b = "Hello";
System.out.println(Objects.equals(a, b));    // Prints "false".
System.out.println(Objects.equals(a, null)); // Prints "true".
// a.equals(b) would throw NullPointerException, but Objects.equals() is safe.

// Use toString() to convert null to a string.
String name = null;
System.out.println(Objects.toString(name));              // Prints "null".
System.out.println(Objects.toString(name, "Anonymous")); // Prints "Anonymous".

// Use requireNonNull() to validate arguments.
class User {
    private String name;

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

try {
    User u = new User(null); // Throws NullPointerException.
} catch (NullPointerException e) {
    System.out.println(e.getMessage()); // Prints "name must not be null.".
}

// Use hash() to implement hashCode.
class Point {
    int x, y;
    Point(int x, int y) { this.x = x; this.y = y; }

    @Override
    public int hashCode() {
        return Objects.hash(x, y); // Computes a hash code from multiple fields concisely.
    }
}

Notes

Objects.equals() is commonly used in equals() implementations because it handles null safely. The standard a.equals(b) throws a NullPointerException when a is null, but Objects.equals(a, b) handles that case safely.

requireNonNull() is used to validate arguments in constructors and public methods, allowing you to detect errors early at the point where null should never appear. Since Java 9, you can also use Objects.requireNonNullElse(obj, defaultValue) to provide a default value when null is passed.

For type checking, see getClass() / instanceof.

If you find any errors or copyright issues, please .