Srikanth Technologies

Objects Class in Java 7.0

Java 7 provides a new class - java.util.Objects. This class provides static methods that provide null-safe or null-tolerant operations on objects.

Consider the following program in Java 6.0.

class  Person {
    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));
    }
}
The above program results in the following output :

Srikanth Pragada
null
Exception in thread "main" java.lang.NullPointerException
	at Person.equals(ObjectsDemo.java:11)
	at ObjectsDemo.main(ObjectsDemo.java:31)
NullPointerException 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, it will result in NullPointerException.

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 <T> int compare(T a, T b, Comparator<? super T> c) 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 <T> T requireNonNull(T obj) Checks that the specified object reference is not null
static <T> T requireNonNull(T obj, String message) 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;

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);
    }
}

When you run the above program the following output will be generated.
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.<init>(ObjectsClass.java:7)
	at ObjectsClass.main(ObjectsClass.java:42)

Post Your Comment

Image

Enter the code given in the above image :
Enter Your Name :
Enter Your Email Address :
Enter Your Comment :

Comments


Posted By Manogna On 07-Jul-11 11:02:07 PM

Perfect one, sir.. You have considered every possible case. Happy to learn things in this way through programs by comparing the two codes.



Posted By SIVA GANESH VARMA On 20-Jul-11 04:12:57 PM

Sir, is it right time to right java 6 certification or else shall i wait for java 7..
Thank you sir.