Primitive wrapper class

From Infogalactic: the planetary knowledge core
Jump to: navigation, search

A primitive - wrapper class in the Java and ActionScript programming languages is one of eight classes provided in the java.lang package to provide object methods for the eight primitive types. All of the primitive wrapper classes in Java are immutable. J2SE 5.0 introduced autoboxing 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 Object is required. The wrapper classes are used extensively with Collection classes in the java.util package and with the classes in the java.lang.reflect reflection package.

The primitive wrapper classes and their corresponding primitive types are:

Primitive type Wrapper class Constructor Arguments
byte Byte byte or String
short Short short or String
int Integer int or String
long Long long or String
float Float float, double or String
double Double double or String
char Character char
boolean Boolean boolean or String

Don't get confused
The term mentioned Primitive Wrapper Classes does not really mean that Wrapper classes are Primitive types in Java. It should be read this way, a class that wraps a primitive type. Wrapper classes can be used to store the same value as of a primitive type variable but the instances/objects of wrapper classes themselves are Non-Primitive. We cannot say that Wrapper classes themselves are Primitive types. They just wrap the primitive types.

The Byte, Short, Integer, Long, Float, and Double wrapper classes are all subclasses of the Number class.

The wrapper classes BigDecimal and BigInteger are not one of the primitive wrapper classes but are immutable.[1] [2]


Although it is a wrapper class, the Void class is similar in that it provides an object representation of the void return. "The Void class is an uninstantiable placeholder class used by the java.lang.reflect API to hold a reference to the Class object representing the Java keyword void."(Javadoc for Void)

Atomic wrapper classes

With Java 5.0, additional wrapper classes were introduced in the 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 operations for addition, increment and assignment.

The atomic wrapper classes and their corresponding types are:

Primitive type Wrapper class
int AtomicInteger
long AtomicLong
boolean AtomicBoolean
V AtomicReference<V>

The AtomicInteger and AtomicLong classes are subclasses of the Number class. The AtomicReference class accepts the type parameter V that specifies the type of the object reference. (See "Generics in Java" for a description of type parameters in Java). V

See also


  1. David O'Meara (2003-04). "Mutable and Immutable Objects: Which classes are Immutable?". Java Ranch. Retrieved 2012-05-14. The classes java.math.BigInteger and BigDecimal are not immutable either, although maybe they should have been. Check date values in: |date= (help)<templatestyles src="Module:Citation/CS1/styles.css"></templatestyles>
  2. Oracle. "Java documentation from oracle". Immutable arbitrary-precision integers.<templatestyles src="Module:Citation/CS1/styles.css"></templatestyles>