![]() |
VOOZH | about |
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).
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
Syntax :Output:
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.
Pair {key = GFG, value = geeksforgeeks.org}
Pair {key = Code, value = no value}
Syntax :Output:
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
true
false
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
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
Syntax :Output:
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
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)
Syntax :Output:
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
450903651
2105869
0
@OverrideNote: 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).
public int hashCode() {
return Objects.hash(x, y, z);
}
Syntax :
public static int hash(Object... values)
Parameters :
values - the values to be hashed
Returns :
a hash value of the sequence of input values
453150372
65282900
961
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.