Thursday, December 1, 2011

New useful class (java.util.Objects) in java 7

                In JAVA 7 API, we'll find a new class: java.util.Objects. This class consists of static utility 9 methods for operating on objects. These methods provide null-safe or null-tolerant operations on objects.
Methods in Objects class :
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

The following Examples demonstrates how to use some of these methods.

import java.util.Objects;

class Employee {

public String empName, empNo;

public Employee(String empName, String empNo){
this.empName = Objects.requireNonNull(empName, "Empolyee Name not be null"); // if first argument null, will throw null pointer exception with customized message
this.empNo = empNo;
}

@Override
public boolean equals(Object obj) {
final Employee employee = (Employee)obj;
return this.empName.equals(employee.empName) && this.empNo.equals(employee.empNo);
}

@Override
public int hashCode() {
return super.hashCode();
}

public void empDetails(){
System.out.println(empName);
System.out.println(Objects.toString(empNo, "Default value"));  // if first argument null, will return the default value
}
}


public class ObjectsDemo{

public static void main(String[] args) {

System.out.println(Objects.hashCode(null)); // display  0 

//Case1
Employee emp1 = new Employee("Employee1","1");
emp1.empDetails();

//Case2
Employee emp2 = new Employee("Employee2", null);
emp2.empDetails();

//Case3
System.out.println(Objects.equals(emp2, null));

//Case4
Employee emp3 = new Employee (null, "4");
emp3.empDetails();


}
}

output will be

0
Employee1
1
Employee2
Default value
Exception in thread "main" java.lang.NullPointerException
at test.Employee.equals(ObjectsDemo.java:18)
at java.util.Objects.equals(Objects.java:57)
at test.ObjectsDemo.main(ObjectsDemo.java:48)

No comments:

Post a Comment