Java Objects Class Example
The Objects
utility class is part of java.util
package and was introduced in Java 1.7. It provides static methods that allow developers to perform operations on objects, handle null values gracefully, and simplify common tasks such as object comparison, hash code generation, and null-safe toString() implementations.
In this blog, we will explore the various methods available in the Objects
class, along with examples to demonstrate their usage.
1. equals(Object a, Object b)
The equals
method compares two objects for equality and returns a Boolean
value indicating whether they are equal or not. It handles null
values gracefully, allowing you to compare objects even when they are null.
String str1 = "Hello";
String str2 = "Hello";
String str3 = "World";
boolean isEqual1 = Objects.equals(str1, str2);
// Output: true
boolean isEqual2 = Objects.equals(str1, str3);
// Output: false
boolean isEqual3 = Objects.equals(null, str1);
// Output: false
2. deepEquals(Object a, Object b)
The deepEquals
method is similar to equals
, but it performs a deep comparison of array elements and nested objects. It can handle multidimensional arrays and recursively compares the elements of the array or objects.
int[] arr1 = {1, 2, 3};
int[] arr2 = {1, 2, 3};
int[] arr3 = {3, 2, 1};
boolean isDeepEqual1 = Objects.deepEquals(arr1, arr2);
// Output: true
boolean isDeepEqual2 = Objects.deepEquals(arr1, arr3);
// Output: false
3. hashCode(Object o)
The hashCode
method returns the hash code of the specified object. It handles null
values and ensures that equal objects have the same hash code.
String str = "Hello";
int hashCode = Objects.hashCode(str);
System.out.println(hashCode); // Output: 69609650
// When a string is NULL
String name = null;
int hashCode1 = Objects.hashCode(name);
System.out.println(hashCode1); // Output: 0
4. toString(Object o)
The toString
method returns a string representation of the specified object. If the object is null
, it returns the string "null"
.
String str = "Hello";
String strRepresentation = Objects.toString(str);
System.out.println(strRepresentation);
// Output: Hello
String nullStr = null;
String nullStrRepresentation = Objects.toString(nullStr);
System.out.println(nullStrRepresentation);
// Output: null
5. isNull(Object obj) and nonNull(Object obj)
The isNull
method checks whether the specified object is null
and returns a Boolean
value accordingly. Conversely, the nonNull
method checks if the object is not null
.
String str = "Hello";
boolean isNull = Objects.isNull(str);
// Output: false
boolean isNonNull = Objects.nonNull(str);
// Output: true
6. requireNonNull(T obj) and requireNonNull(T obj, String message)
The requireNonNull
method checks if the specified object is null
and throws a NullPointerException
if it is. The second variant allows you to provide a custom error message.
String str = "Hello";
String nullStr = null;
Objects.requireNonNull(str);
// No exception thrown
Objects.requireNonNull(nullStr, "The object cannot be null!");
// Output: NullPointerException with the specified error message
7. compare(T a, T b, Comparator<? super T> c)
The compare
method compares two objects of type T using the specified Comparator
. It returns an integer value indicating the relationship between the two objects: negative if a is less than b, positive if a is greater than b, and zero if they are equal.
String str1 = "Apple";
String str2 = "Banana";
int result1 = Objects.compare(str1, str2, String.CASE_INSENSITIVE_ORDER);
// Output: a negative value since "Apple" comes before "Banana" in a case-insensitive comparison
int result2 = Objects.compare(str2, str1, String.CASE_INSENSITIVE_ORDER);
// Output: a positive value since "Banana" comes after "Apple" in a case-insensitive comparison
int result3 = Objects.compare(str1, str1, String.CASE_INSENSITIVE_ORDER);
// Output: 0 since both strings are equal in a case-insensitive comparison
The compare
method is particularly useful when you need to sort or order objects based on a specific criterion provided by a Comparator
.