Objects Class is a helper class to work with Object class.
Here are some useful methods from Objects class.
Method | Description |
---|---|
compare() | 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. |
deepEquals() | 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. |
equals() | 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. |
hash() | 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. |
hashCode() | returns the hash code value of the specified object. If the argument is null, it returns 0. |
isNull() | returns true if the specified object is null. Otherwise, it returns false. |
nonNull() | performs the check opposite of what the isNull() method does. |
requireNonNull(T obj) | method checks if the argument is not null. If the argument is null, it throws a NullPointerException. |
toString() | 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. |
A Test Class to Demonstrate the Use of the Methods of the Objects Class
import java.time.Instant; import java.util.Objects; import java.util.function.Supplier; public class Main { public static void main(String[] args) { // Compute hash code for two integers, a char, and a string int hash = Objects.hash(10, 8900, '\u20b9', "Hello"); System.out.println("Hash Code is " + hash); // Test for equality boolean isEqual = Objects.equals(null, null); System.out.println("null is equal to null: " + isEqual); isEqual = Objects.equals(null, "XYZ"); System.out.println("null is equal to XYZ: " + isEqual); // toString() method test System.out.println("toString(null) is " + Objects.toString(null)); System.out.println("toString(null, \"XXX\") is " + Objects.toString(null, "XXX")); // Testing requireNonNull(T obj, String message) try {// www .ja va 2 s . c om printName("abc"); printName(null); } catch (NullPointerException e) { System.out.println(e.getMessage()); } // requireNonNull(T obj, Supplier<String> messageSupplier) try { // Using a lambda expression to create a Supplier<String> object. // The Supplier returns a timestamped message. Supplier<String> messageSupplier = () -> "Name is required. Error generated on " + Instant.now(); printNameWithSuplier("abc", messageSupplier); printNameWithSuplier(null, messageSupplier); } catch (NullPointerException e) { System.out.println(e.getMessage()); } } public static void printName(String name) { // Test name for not null. Generate a NullPointerException if it is null. Objects.requireNonNull(name, "Name is required."); // Print the name if the above statement did not throw an exception System.out.println("Name is " + name); } public static void printNameWithSuplier(String name, Supplier<String> messageSupplier) { // Test name for not null. Generate a NullPointerException if it is null. Objects.requireNonNull(name, messageSupplier); // Print the name if the above statement did not throw an exception System.out.println("Name is " + name); } }