Inobject-oriented programming,awrapper classis aclassthatencapsulatestypes,so that those types can be used to createobjectinstancesandmethodsin another class that needs those types. So aprimitive wrapper classis a wrapper class that encapsulates, hides orwrapsdata types from the eightprimitive data types,[1]so that these can be used to create instantiated objects with methods in another class or in other classes.[2][3]The primitive wrapper classes are found in theJava API.
Primitive wrapper classes are used to create anObject
that needs to represent primitive types inCollection
classes (i.e., in the Java API), in thejava.util
package and in thejava.lang.reflect
reflectionpackage. Collection classes are Java API-defined classes that can store objects in a manner similar to how data structures like arrays store primitive data types likeint,double,longorchar,etc.,[2]but arrays store primitive data types while collections actually store objects.
The primitive wrapper classes and their corresponding primitive types are:
The difference between wrapper classes and primitive types
editPrimitive wrapper classes are not the same thing as primitive types. Whereas variables, for example, can be declared in Java as data typesdouble,short,int,etc., the primitive wrapper classes create instantiated objects and methods that inherit but hide the primitive data types, not like variables that are assigned the data type values.[2]
Therefore, the termPrimitive wrapper classdoes not mean that wrapper classes are primitive types. It should be understood to be a class that wraps primitive types. Wrapper classes can be used to store the same value as of a primitive type variable but the instances/objects of wrapper classes themselves areNon-Primitive.We cannot say that Wrapper classes themselves are Primitive types. They just wrap the primitive types.
TheByte
,Short
,Integer
,Long
,Float
,andDouble
wrapper classes are allsubclassesof theNumber
class.
The wrapper classesBigDecimal
andBigInteger
are not one of the primitive wrapper classes but are immutable.[4]
[5]
Atomic wrapper classes
editWith Java 5.0, additional wrapper classes were introduced in thejava.util.concurrent.atomic
package. These classes are mutable and cannot be used as a replacement for the regular wrapper classes. Instead, they provideatomic operationsfor 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>
TheAtomicInteger
andAtomicLong
classes are subclasses of theNumber
class. TheAtomicReference
class accepts thetype parameterV
that specifies the type of the objectreference.(See "Generics in Java"for a description of type parameters in Java).
V
See also
editReferences
edit- ^S. J. Chapman,Introduction to Java,Prentice Hall, 1999.
- ^abcJ. Murach,Murach's Java Programming,4th Edition, Mike Murach and Associates, Inc., 2011.
- ^J. R. Hubbard,Programming with Java,Schaum's Outline Series/McGraw Hill, 1998.
- ^David O'Meara (April 2003)."Mutable and Immutable Objects: Which classes are Immutable?".Java Ranch.Retrieved2012-05-14.
The classes java.math.BigInteger and BigDecimal are not immutable either, although maybe they should have been.
- ^Oracle."Java documentation from oracle".
Immutable arbitrary-precision integers.