- Primitive wrapper class
A primitive wrapper class in the Java programming language is one of eight classes provided in the Javadoc:SE|package=java. _ja. package] to provide object methods for the eight
primitive type s. All of the primitive wrapper classes in Java are immutable.J2SE 5.0 introducedautoboxing of primitive types into their wrapper object, and automatic unboxing of the wrapper objects into their primitive value—the implicit conversion between the wrapper objects and primitive values.Wrapper classes are used to represent primitive values when an Javadoc:SE|java/lang|Object is required. The wrapper classes are used extensively with Javadoc:SE|java/util|Collection classes in the Javadoc:SE|package=java.util|java/util package and with the classes in the Javadoc:SE|package=java.lang.reflect|java/lang/reflect reflection package.
The primitive wrapper classes and their corresponding primitive types are:
:
The
Byte
,Short
,Integer
,Long
,Float
, andDouble
wrapper classes are allsubclasses of the Javadoc:SE|java/lang|Number class.Use of primitive wrappers in reflection
When looking for methods with a particular signature, each argument of the method signature must be defined using its corresponding Class type. To be able to define primitive arguments, each primitive type is associated with an immutable Class that represent this type when using reflection.
Suppose a class "Foo" with one method called "doSomething", having only one primitive "int" parameter. If we want to call this method using reflection, we will have to use the Type associated with the "int" primitive:
Void
Although it is not a wrapper class, the Javadoc:SE|java/lang|Void class is similar in that it provides an object representation of the
void
return type. TheVoid
class is an uninstantiable placeholder class used by the Javadoc:SE|package=java.lang.reflect|java/lang/reflect API to hold a reference to the Javadoc:SE|java/lang|Class object representing theJava keyword void
.Atomic wrapper classes
With Java 5.0, additional wrapper classes were introduced in the Javadoc:SE|package=java.util.concurrent.atomic|java/util/concurrent/atomic package. These classes are mutable and cannot be used as a replacement for the regular wrapper classes. Instead, they provide
atomic operation s for addition, increment and assignment.The atomic wrapper classes and their corresponding types are:
:
The
AtomicInteger
andAtomicLong
classes are subclasses of theNumber
class. TheAtomicReference
class accepts the type parameterV
that specifies the type of the object reference. (See "Generics in Java " for a description of type parameters in Java).See also
*
Java Platform, Standard Edition#java. _ja. java.lang.reflect
* Java programming language
*Java syntax
Wikimedia Foundation. 2010.