Getting default value for primitive types
The Guava Libraries already contains that:
http://guava-libraries.googlecode.com/svn/trunk/javadoc/com/google/common/base/Defaults.html
Calling defaultValue
will return the default value for any primitive type (as specified by the JLS), and null for any other type.
Use it like so:
import com.google.common.base.Defaults;
Defaults.defaultValue(Integer.TYPE); //will return 0
It's possible to get the default value of any type by creating an array of one element and retrieving its first value.
private static <T> T getDefaultValue(Class<T> clazz) {
return (T) Array.get(Array.newInstance(clazz, 1), 0);
}
This way there is not need to take account for every possible primitive type, at the usually negligible cost of creating a one-element array.