JDK 7: Small Library Treats
By darcy on Aug 18, 2011
With JDK 7 out the door, many writeups of the release have naturally focused on its larger features like Project Coin, NIO.2, the fork/join framework, and
invokedynamic. However, there are a number of small but useful library conveniences in JDK 7 that shouldn't be overlooked.
Library changes I think are handy include:
To allow more informative assertion errors to be constructed,
AssertionErrornow includes a constructor taking a string message and a
Throwablecause, which is one of the four conventional constructors for exception types.
The methods which convert strings to numeric values differed in whether or not a leading plus,
"+", was regarded as valid input.
BigDecimalaccepted a leading plus while
BigIntegerdid not. As of JDK 7, all those types consistently accept a leading plus in their
To help write more efficient comparators,
Longeach have a two-argument static
comparemethod that performs the desired comparison operation without boxing overhead.
This six checked exception types that can be thrown by core reflection now have a common supertype,
ReflectiveOperationException. This additional type complements the improved exception handling available with multi-catch and more precise re-throw from Project Coin.
java.util.Objectsclass contains a number of small, but commonly used methods, many of them for coping with null inputs. For example,
Objectscontains a two-argument static
equalsmethod which does the right thing on null inputs. I've replaced a half-dozen copies of that method's logic in the JDK; I'm sure many more instances can be removed in other code bases as JDK 7 is adopted.
Part of NIO.2, I expect over time the utility methods in
java.nio.file.Filesto copy, read, and write the contents of file will see heavy usage.