Consider the following program in Java 6.0.
class Person {The above program results in the following program:
private String name, email;
public Person(String name, String email) {
this.name = name;
this.email = email;
}
@Override
public boolean equals(Object obj) {
final Person other = (Person) obj;
return this.name.equals(other.name) && this.email.equals(other.email);
}
@Override
public int hashCode() {
return 1;
}
public void print(){
System.out.println (name);
System.out.println (email);
}
}
public class ObjectsDemo {
public static void main(String[] args) {
Person p1 = new Person("Srikanth Pragada", null);
Person p2 = new Person("Srikanth Pragada", "srikanthpragada@gmail.com");
p1.print();
System.out.println(p1.equals(p2));
}
}
Srikanth PragadaNullPointerException is thrown in equals() method as we are using email, which is set to null, for comparison. It means, if any variable is set to null, unless we explicitly handle null, will result in NullPointerException.
null
Exception in thread "main" java.lang.NullPointerException
at Person.equals(ObjectsDemo.java:11)
at ObjectsDemo.main(ObjectsDemo.java:31)
This problem of NullPointerException can be prevented by using null-safe operations using Java 7.0 Objects class.
The following are the static methods provided by Objects class.
Method | Description |
---|---|
static | Returns 0 if the arguments are identical and c.compare(a, b) otherwise |
static boolean deepEquals(Object a, Object b) | Returns true if the arguments are deeply equal to each other and false otherwise |
static boolean equals(Object a, Object b) | Returns true if the arguments are equal to each other and false otherwise |
static int hash(Object... values) | Generates a hash code for a sequence of input values. |
static int hashCode(Object o) | Returns the hash code of a non-null argument and 0 for a null argument |
static | Checks that the specified object reference is not null |
static | Checks that the specified object reference is not null and throws a customized NullPointerException if it is |
static String toString(Object o) | Returns the result of calling toString for a non-null argument and "null" for a null argument |
static String toString(Object o, String nullDefault) | Returns the result of calling toString on the first argument if the first argument is not null and returns the second argument otherwise |
The following program demonstrates how to use some of these methods.
import java.util.Objects;When you run the above program the following output will be generated.
class Person {
private String name, email;
public Person(String name, String email) {
this.name = Objects.requireNonNull(name,"Name must be not null");
this.email = email;
}
public boolean equals(Object obj) {
final Person other = (Person) obj;
return this.name.equals(other.name) && this.email.equals(other.email);
}
public int hashCode() {
return 1;
}
public void print(){
System.out.println (name);
System.out.println (Objects.toString(email,"Email is null"));
// returns second parameter if email is null
}
}
public class ObjectsClass {
public static void main(String[] args) {
Person p1 = new Person("abc","abc@gmail.com");
p1.print();
Person p2 = null;
System.out.println(Objects.equals(p2,p1));
// null-save equals, if first parameter is null it returns false
p2 = new Person("abc","abc@gmail.com");
System.out.println(Objects.equals(p1, p2)); // null-save equals
Person p3 = new Person("Srikanth Pragada",null);
p3.print();
Person p4 = new Person(null,null);
}
}
abc
abc@gmail.com
false
true
Srikanth Pragada
Email is null
Exception in thread "main" java.lang.NullPointerException: Name must be not null
at java.util.Objects.requireNonNull(Objects.java:226)
at Person.(ObjectsClass.java:7)
at ObjectsClass.main(ObjectsClass.java:42)
No comments:
Post a Comment