Primitive wrapper class
In object oriented programming, a wrapper class is a class that encapsulates types, so that those types can be used to create object instances and methods in another class that need those types. So a primitive wrapper class is a wrapper class that encapsulates, hides or wraps data types from the eight primitive 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 the Java API.
Primitive wrapper classes are used to create an Object
that needs to represent primitive types in Collection
classes (i.e., in the Java API), in the java.util
package and in the java.lang.reflect
reflection package. 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 like, int, double, long or char, etc.,[4] But arrays store primitive data types while collections actually store objects.
The primitive wrapper classes and their corresponding primitive types are:
Primitive type Wrapper class Constructor Arguments byte
Byte
byte
orString
short
Short
short
orString
int
Integer
int
orString
long
Long
long
orString
float
Float
float
,double
orString
double
Double
double
orString
char
Character
char
boolean
Boolean
boolean
orString
The Difference between Wrapper Classes and Primitive Types
Primitive wrapper classes are not the same thing as are primitive types. The main difference is that whereas variables can be declared in Java as double, short, int, or char, etc., data types, the eight primitive wrapper classes create instantiated objects and methods that inherit but hide the eight primitive data types, not variables that are assigned data type values.[5]
Therefore, the term Primitive wrapper class does 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 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.[6]
[7]
The Wrapper Class Void
The wrapper class named Void is not a primitive wrapper class, specifically because void is not a primitive data type. However the Void wrapper class can be used to create new objects that have methods that are not expected to return a value.
Although it is a wrapper class, the Void
class provides an object representation of the void
return.
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
References
- ↑ S. J., Chapman, Introduction to Java, Prentice Hall, 1999.
- ↑ J. 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.
- ↑ J. Murach, Murach's Java Programming, 4th Edition, Mike Murach and Associates, Inc., 2011.
- ↑ J. Murach, Murach's Java Programming, 4th Edition, Mike Murach and Associates, Inc., 2011.
- ↑ 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) - ↑ Oracle. "Java documentation from oracle".
Immutable arbitrary-precision integers.