Java 7 has come up with a new class Objects that have 9 static utility methods for operating on objects. These utilities include null-safe methods for computing the hash code of an object, returning a string for an object, and comparing two objects.
Using Objects class methods, one can smartly handle NullPointerException and can also show customized NullPointerException message(if an Exception occur).
- String toString(Object o) : This method returns the result of calling toString() method for a non-null argument and “null” for a null argument.
Syntax : public static String toString(Object o) Parameters : o - an object Returns : the result of calling toString() method for a non-null argument and "null" for a null argument
- String toString(Object o, String nullDefault) : This method is overloaded version of above method. It returns the result of calling toString() method on the first argument if the first argument is not null and returns the second argument otherwise.
Syntax : public static String toString(Object o, String nullDefault) Parameters : o - an object nullDefault - string to return if the first argument is null Returns : the result of calling toString() method on the first argument if it is not null and the second argument otherwise.
// Java program to demonstrate Objects.toString(Object o)
// and Objects.toString(Object o, String nullDefault) methods
import
java.util.Objects;
class
Pair<K, V>
{
public
K key;
public
V value;
public
Pair(K key, V value)
{
this
.key = key;
this
.value = value;
}
@Override
public
String toString() {
return
"Pair {key = "
+ Objects.toString(key) +
", value = "
+
Objects.toString(value,
"no value"
) +
"}"
;
/* without Objects.toString(Object o) and
Objects.toString(Object o, String nullDefault) method
return "Pair {key = " + (key == null ? "null" : key.toString()) +
", value = " + (value == null ? "no value" : value.toString()) + "}"; */
}
}
class
GFG
{
public
static
void
main(String[] args)
{
Pair<String, String> p1 =
new
Pair<String, String>(
"GFG"
,
"geeksforgeeks.org"
);
Pair<String, String> p2 =
new
Pair<String, String>(
"Code"
,
null
);
System.out.println(p1);
System.out.println(p2);
}
}
Output:
Pair {key = GFG, value = geeksforgeeks.org} Pair {key = Code, value = no value}
- boolean equals(Object a,Object b) : This method true if the arguments are equal to each other and false otherwise. Consequently, if both arguments are null, true is returned and if exactly one argument is null, false is returned. Otherwise, equality is determined by using the equals() method of the first argument.
Syntax : public static boolean equals(Object a,Object b) Parameters : a - an object b - an object to be compared with a for equality Returns : true if the arguments are equal to each other and false otherwise
// Java program to demonstrate equals(Object a, Object b) method
import
java.util.Objects;
class
Pair<K, V>
{
public
K key;
public
V value;
public
Pair(K key, V value)
{
this
.key = key;
this
.value = value;
}
@Override
public
boolean
equals(Object o)
{
if
(!(o
instanceof
Pair)) {
return
false
;
}
Pair<?, ?> p = (Pair<?, ?>) o;
return
Objects.equals(p.key, key) && Objects.equals(p.value, value);
}
}
class
GFG
{
public
static
void
main(String[] args)
{
Pair<String, String> p1 =
new
Pair<String, String>(
"GFG"
,
"geeksforgeeks.org"
);
Pair<String, String> p2 =
new
Pair<String, String>(
"GFG"
,
"geeksforgeeks.org"
);
Pair<String, String> p3 =
new
Pair<String, String>(
"GFG"
,
"www.geeksforgeeks.org"
);
System.out.println(p1.equals(p2));
System.out.println(p2.equals(p3));
}
}
Output:
true false
- boolean deepEquals(Object a,Object b) :This method returns true if the arguments are deeply equal to each other and false otherwise. Two null values are deeply equal. If both arguments are arrays, the algorithm in Arrays.deepEquals is used to determine equality. Otherwise, equality is determined by using the equals method of the first argument.
Syntax : public static boolean deepEquals(Object a,Object b) Parameters : a - an object b - an object to be compared with a for equality Returns : true if the arguments are deeply equals to each other and false otherwise
- T requireNonNull(T obj) : This method checks that the specified object reference is not null. This method is designed primarily for doing parameter validation in methods and constructors, as demonstrated in below example:
Syntax : public static T requireNonNull(T obj) Type Parameters: T - the type of the reference Parameters : obj - the object reference to check for nullity Returns : obj if not null Throws: NullPointerException - if obj is null
- T requireNonNull(T obj,String message) : This method is overloaded version of above method with customized message printing if obj is null as demonstrated in below example:
Syntax : public static T requireNonNull(T obj,String message) Type Parameters: T - the type of the reference Parameters : obj - the object reference to check for nullity message - detail message to be used in the event that a NullPointerException is thrown Returns : obj if not null Throws: NullPointerException - if obj is null
// Java program to demonstrate Objects.requireNonNull(Object o)
// and Objects.requireNonNull(Object o, String message) methods
import
java.util.Objects;
class
Pair<K, V>
{
public
K key;
public
V value;
public
Pair(K key, V value)
{
this
.key = key;
this
.value = value;
}
public
void
setKey(K key) {
this
.key = Objects.requireNonNull(key);
}
public
void
setValue(V value) {
this
.value = Objects.requireNonNull(value,
"no value"
);
}
}
class
GFG
{
public
static
void
main(String[] args)
{
Pair<String, String> p1 =
new
Pair<String, String>(
"GFG"
,
"geeksforgeeks.org"
);
p1.setKey(
"Geeks"
);
// below statement will throw NPE with customized message
p1.setValue(
null
);
}
}
Output:
Exception in thread "main" java.lang.NullPointerException: no value at java.util.Objects.requireNonNull(Objects.java:228) at Pair.setValue(GFG.java:22) at GFG.main(GFG.java:36)
- int hashCode(Object o) : This method returns the hash code of a non-null argument and 0 for a null argument.
Syntax : public static int hashCode(Object o) Parameters : o - an object Returns : the hash code of a non-null argument and 0 for a null argument
// Java program to demonstrate Objects.hashCode(Object o) object
import
java.util.Objects;
class
Pair<K, V>
{
public
K key;
public
V value;
public
Pair(K key, V value)
{
this
.key = key;
this
.value = value;
}
@Override
public
int
hashCode()
{
return
(Objects.hashCode(key) ^ Objects.hashCode(value));
/* without Objects.hashCode(Object o) method
return (key == null ? 0 : key.hashCode()) ^
(value == null ? 0 : value.hashCode()); */
}
}
class
GFG
{
public
static
void
main(String[] args)
{
Pair<String, String> p1 =
new
Pair<String, String>(
"GFG"
,
"geeksforgeeks.org"
);
Pair<String, String> p2 =
new
Pair<String, String>(
"Code"
,
null
);
Pair<String, String> p3 =
new
Pair<String, String>(
null
,
null
);
System.out.println(p1.hashCode());
System.out.println(p2.hashCode());
System.out.println(p3.hashCode());
}
}
Output:
450903651 2105869 0
- int hash(Object… values) : This method generates a hash code for a sequence of input values. The hash code is generated as if all the input values were placed into an array, and that array were hashed by calling Arrays.hashCode(Object[]).
This method is useful for implementing Object.hashCode() on objects containing multiple fields. For example, if an object that has three fields, x, y, and z, one could write:@Override public int hashCode() { return Objects.hash(x, y, z); }
Note: When a single object reference is supplied, the returned value does not equal the hash code of that object reference. This value can be computed by calling hashCode(Object).
Syntax : public static int hash(Object... values) Parameters : values - the values to be hashed Returns : a hash value of the sequence of input values
// Java program to demonstrate Objects.hashCode(Object o) object
import
java.util.Objects;
class
Pair<K, V>
{
public
K key;
public
V value;
public
Pair(K key, V value)
{
this
.key = key;
this
.value = value;
}
@Override
public
int
hashCode()
{
return
(Objects.hash(key,value));
}
}
class
GFG
{
public
static
void
main(String[] args)
{
Pair<String, String> p1 =
new
Pair<String, String>(
"GFG"
,
"geeksforgeeks.org"
);
Pair<String, String> p2 =
new
Pair<String, String>(
"Code"
,
null
);
Pair<String, String> p3 =
new
Pair<String, String>(
null
,
null
);
System.out.println(p1.hashCode());
System.out.println(p2.hashCode());
System.out.println(p3.hashCode());
}
}
Output:
453150372 65282900 961
- int compare(T a,T b,Comparator c) : As usual, this method returns 0 if the arguments are identical and c.compare(a, b) otherwise. Consequently, if both arguments are null 0 is returned.
Note that if one of the arguments is null, a NullPointerException may or may not be thrown depending on what ordering policy, if any, the Comparator chooses to have for null values.
Syntax : public static int compare(T a,T b,Comparator c) Type Parameters: T - the type of the objects being compared Parameters : a - an object b - an object to be compared with a c - the Comparator to compare the first two arguments Returns : 0 if the arguments are identical and c.compare(a, b) otherwise.
Note : In Java 8, Objects class has 3 more methods. Two of them(isNull(Object o) and nonNull(Object o)) are used for checking null reference. The third one is one more overloaded version of requireNonNull method. Refer here.