Java Reference
In-Depth Information
<T> T requireNonNull(T obj)
•
<T> T requireNonNull(T obj, String message)
•
<T> T requireNonNull(T obj, Supplier<String> messageSupplier)
•
String toString(Object o)
•
String toString(Object o, String nullDefault)
The
compare()
method is used to compare two objects for sorting purpose. It returns 0 if both arguments are
identical. Otherwise, it returns the value of
c.compare(a, b)
. It returns 0 if both arguments are
null
.
The
deepEquals()
method is used to check if two objects are deeply equal. It returns
true
if both arguments are
deeply equal. Otherwise, it returns
false
. It returns
true
if both arguments are
null
.
The
equals()
method compares two objects for equality. It returns
true
if both arguments are equal. Otherwise,
it returns
false
. It returns
true
if both arguments are
null
.
The
hash()
method generates a hash code for all specified objects. It can be used to compute the hash code for
an object, which is based on the multiple instance fields. If a single object reference is passed to this method, the
returned hash code value is not equal to the hash code value returned from the object's
hashCode()
method. If
obj
is
an object reference,
obj.hashCode()
is not equal to
Objects.hash(obj)
.
The
hashCode()
method returns the hash code value of the specified object. If the argument is
null
, it returns 0.
The
isNull()
method returns
true
if the specified object is
null
. Otherwise, it returns
false
. You can also check
whether an object is null using the comparison operator
==
, for example,
obj == null
returns true of
obj
is
null
.
The
isNull()
method is added in Java 8. It exists to be used as a method reference (
Objects::isNull
) in lambda
expressions. Lambda expressions are discussed in the topic
Beginning Java Language Features
.
The
nonNull()
method performs the check opposite of what the
isNull()
method does. It is added in Java8 to be
used in lambda expression as a method reference (
Objects::nonNull
).
The
requireNonNull(T obj)
method checks if the argument is not
null
. If the argument is
null
, it throws a
NullPointerException
. This method is designed for validating parameters of methods and constructors. Notice the
formal type parameter
<T>
in the method's declaration. It is a generic method. Any type of object is passed. Its return
type is the same as the type of the passed object. The method is overloaded. The second version of the method lets
you specify the message for the
NullPointerException
that is thrown when the argument is
null
. The third version
of the method takes a
Supplier<String>
as the second argument. This is added in Java 8. It defers the creation of
the message until the null check is performed. If the argument is
null
, the
get()
method of the
Supplier<String>
is
called to get the error message that is used in
NullPointerException
. Use the third argument if you want to add the
timestamp in your error message.
The
toString()
method returns a “null” string if the argument is
null
. For a non-null argument, it returns the
value returned by calling the
toString()
method on the argument. The second version of the method lets you specify
the default retuned string when the argument is null.
Listing 7-22 demonstrates how to use some of the methods of the
Objects
class. The program uses a lambda
expression to create a
Supplier<String>
object. Lambda expressions are discussed in Chapter 5 in book
Beginning
Java Language Features
. You may get a different output when you run the program.
•
Listing 7-22.
A Test Class to Demonstrate the Use of the Methods of the Objects Class
// ObjectsTest.java
package com.jdojo.object;
import java.time.Instant;
import java.util.Objects;
import java.util.function.Supplier;