Primitive wrapper class in Java
| Industry | Software development |
|---|---|
| Products | Primitive wrapper classes, atomic wrapper classes |
In object-oriented programming, a wrapper class refers to a class that encapsulates specific types, allowing these types to be instantiated as objects and to provide methods within other classes. A primitive wrapper class is designed to encapsulate the eight fundamental primitive data types, enabling them to function as objects with associated methods. These classes are included in the Java API.
Primitive wrapper classes enable primitive types to be represented as objects within Java API classes, particularly in the and packages. Since collection classes require objects rather than direct primitive values, wrapper classes are essential for primitive types such as int, double, long, and char, unlike arrays that hold primitives directly.
The following table lists the primitive types alongside their corresponding wrapper classes and the constructor parameters they accept:
Primitive type Wrapper class Constructor arguments bytebyteorStringshortshortorStringintintorStringlonglongorStringfloatfloat,double, orStringdoubledoubleorStringcharcharbooleanbooleanorString
Distinctions Between Wrapper Classes and Primitive Types
Primitive wrapper classes are distinct from primitive data types. Variables declared as int, double, short, etc., represent primitives directly, whereas wrapper classes instantiate objects that enclose these primitive values, offering methods and abstracting the raw data. Instances of wrapper classes are considered non-primitive despite encapsulating values equivalent to primitive variables.
The designation primitive wrapper class pertains to a class that encapsulates primitive types rather than implying that the wrapper itself is a primitive.
Classes such as Byte, Short, Integer, Long, Float, and <code<Double></nowiki></nowiki></nowiki></nowiki></nowiki></nowiki></nowiki></nowiki></nowiki></nowiki></nowiki></nowiki></nowiki></nowiki></nowiki></nowiki></nowiki></nowiki></nowiki></nowiki></nowiki></nowiki></nowiki></nowiki></nowiki></nowiki></nowiki></nowiki></nowiki></nowiki></nowiki></nowiki></nowiki></nowiki></nowiki>
These classes are all subclasses of the class.
Moreover, although not classified as primitive wrappers, the immutable classes BigDecimal and BigInteger offer high-precision numeric representations crucial for business and financial computations.
Atomic Wrapper Classes
Starting with Java 5.0, the package introduced atomic wrapper classes that support mutable atomic operations such as incrementing, addition, and assignment. These atomic wrappers complement traditional wrapper classes but do not replace them.
The atomic wrappers along with their associated primitive types include:
Primitive type Wrapper class intlongbooleanV
Both AtomicInteger and AtomicLong extend the abstract class Number</ code>, while < code >AtomicReference< / code > uses a generic type parameter (< code >V< / code >) to define the referenced object's type.
See also
References