Last Updated : 24 Jul, 2024
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 :Java
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}
Syntax :Java
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
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 :Java
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)
Syntax :Java
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
@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 :Java
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
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.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4