Java 7 is coming, so it’s worth to see how the new version helps to create clearer code. Here I present some simple but very useful methods of java.util.Objects class. All those methods are static, so you don’t have to create a new object. Using them it’s easy reduce “if” statements or conditional expressions. The following examples show an old and a new code.
First method checks if an object is null. Will be most useful in methods’ arguments validation.
Next method helps when you want see object as String but you’re not sure if it isn’t null.
Last method is similar to the one above but for getting hash code. It can be useful when you overwrite the hashCode method.
The methods above help you handle variable which may be null. I hope using this class, developers will reduce number of unexpected NullPointerException.
That isn’t all methods. It is worth to see Objects class in documentation.