Compare two objects with possible 'null' value - java.util.Objects
Java 7 provided the new class called 'Objects'. This class provides a null safe method to compare
two objects.
java.util.Objects
boolean equals(Object a,Object b)
- if both object are null , it will return true
- if one object is null, false will return
- otherwise equality comparison based on equals() method
example :
String s1 = null;
String s2 = "one";
if you apply s1.equals(s2);
Then in this case if s1 is null like here, it will give you nullpointer exception
So for safe comparison we can use.
Objects.equals(s1,s2);
This will return false in our case.
Labels: Compare two objects with possible 'null' value, java.util.Objects
0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home