Thursday 30 June 2011

JDK 1.7 ~java.util.Objects~

Working with Jdk 1.7 early access download,one of the enhancements made is introduction of java.util.Objects similar to that of java.util.Collections.The Objects class supports some null safe and null tolerant methods.Check the following class for some hint.

1.Equals Support



  To know its usefulness let us say your code looks as follows:


     if(foo.equals(foo2)) { //do something }

what if foo is null,you will encounter null pointer exception(else may be you need to have one more check for null),with java.util.Objects


 Objects.equals(foo,foo2)) if foo is null,it will return false :-)






2.Hashing Support:


Most of the standards and technologies need their classed\s to override equals and hashcode method(atleast some times u may need to for better performance e.g. HashMap usage), java.util.objects provides a better way of hashing.



 This is very useful especially for your DTO objects which may contain many number of fields.

 hashCode(Object o):int -->>  Returns the hash code of a non-null argument and 0 for a null argument.


and there is some more enhancements made toString() with null support.

Thats it for now.... cheers ;-)





No comments:

Post a Comment