Getting default value for java primitive types

Posted by ripper234 on Stack Overflow See other posts from Stack Overflow or by ripper234
Published on 2010-05-23T13:51:54Z Indexed on 2010/05/23 14:00 UTC
Read the original article Hit count: 160

Filed under:
|

I have a java primitive type at hand:

Class c = int.class; // or long.class, or boolean.class

I'd like to get a 'default value' for this class - specifically the value is assigned to fields of this type if they are not initialized. E.g., '0' for a number, 'false' for a boolean.

Is there a generic way to do this? I tried

c.newInstance()

But I'm getting an InstantiationException, and not a default instance.

© Stack Overflow or respective owner

Related posts about java

Related posts about reflection