Last Updated on 15/11/2020
The Java Objects class is a utility class introduced in Java 7 and expanded in Java 8 to provide a collection of static methods to help operating on Java objects. It has null-safe and null-tolerant methods for computing the hash code of an object, returning a string for an object, comparing two objects, null checking and more.
According to Brian Goetz (Oracle’s architect for the Java language), its features are also being introduced into the core of the language, so there is a chance the Objects
class becomes more popular in the years that are coming.
That said, we are about to explore how to work with the Objects
class.
Check if a parameter is null
We often have methods or constructors that receive mandatory parameters that we would like to test against null
and throw an exception in case they are null
:
public class ObjectsExample { public void foo(String bar) { if (bar == null) { throw new NullPointerException(); } // Do something with bar... } }
With Objects, the same behaviour is coded in a single line like the following, it’s gonna check if bar
is null and thrown a NullPointerException
if so:
import java.util.Objects; public class ObjectsExample { public void foo(String bar) { Objects.requireNonNull(bar); // Do something with bar... } }
You can also use the overloaded versions of the method to provide additional information like the Exception’s message
:
Objects.requireNonNull(bar, "Bar can't be null!");
Or provide a supplier that will create the Exception’s message
only if necessary:
import java.util.Objects; public class ObjectsExample { public void foo(String bar) { Objects.requireNonNull(bar, this::createErrorMessage); // Do something with bar... } public String createErrorMessage() { return "Bar can't be null!!!"; } }
Safely compare two objects for equality
If you have two objects to compare for equality using Object.equals(Object), you’d need to check if the first argument is null before comparing:
public boolean baz(Object first, Object second) { if (first != null) { return first.equals(second); } return (second == null); }
However, using Objects.equals(Object, Object) you can get rid of the null check. The code below produces the same output as the previously one:
public boolean baz(Object first, Object second) { return Objects.equals(first, second); }
Safely invoke Object.toString()
Sometimes you’d like to print the string representation of an Object using the Object.toString() method. This is sometimes useful when you, e.g. want to print object’s state to the log file after an exception occurred:
logger.error("Xyz error. Foo's state: " + foo.toString());
What if foo
is null
? Our old friend NullPointerException
couldn’t wait to be thrown. So we’d write something like the following to be safe:
logger.error("Xyz error. Foo's state: " + foo == null ? "Foo is null" : foo.toString());
A bit verbose, but we can make it simpler using Objects.toString(Object):
logger.error("Xyz error. Foo's state: " + Objects.toString(foo));
Or use its overloaded version to provide a default message to be print if foo
is null
:
logger.error("Xyz error. Foo's state: " + Objects.toString(foo, "Foo is null"));
It’s not over with Java Objects
That was just an idea of what the Objects
class can do for you. Sometimes we fill our applications with third party libraries seeking for utility methods, but sometimes we are likely to find them in the JDK itself.
To discover more, please refer to the Java Objects javadoc.
I hope it helps.
Cya!