In
object-oriented programming
Object-oriented programming (OOP) is a programming paradigm based on the concept of "objects", which can contain data and code. The data is in the form of fields (often known as attributes or ''properties''), and the code is in the form of ...
, a wrapper class is a
class that
encapsulates types
Type may refer to:
Science and technology Computing
* Typing, producing text via a keyboard, typewriter, etc.
* Data type
In computer science and computer programming, a data type (or simply type) is a set of possible values and a set of allo ...
, so that those types can be used to create
object
Object may refer to:
General meanings
* Object (philosophy), a thing, being, or concept
** Object (abstract), an object which does not exist at any particular time or place
** Physical object, an identifiable collection of matter
* Goal, an ...
instances and
methods in another class that needs those types. So a primitive wrapper class is a wrapper class that encapsulates, hides or ''wraps'' data types from the eight
primitive data type
In computer science, primitive data types are a set of basic data types from which all other data types are constructed. Specifically it often refers to the limited set of data representations in use by a particular processor, which all compiled pr ...
s, so that these can be used to create instantiated objects with methods in another class or in other classes.
[J. Murach, ''Murach's Java Programming'', 4th Edition, Mike Murach and Associates, Inc., 2011.] The primitive wrapper classes are found in the
Java API
There are two types of Java programming language application programming interfaces (APIs):
* The official core Java API, contained in the Android (Google), SE (OpenJDK and Oracle), MicroEJ. These packages (java.* packages) are the core Java la ...
.
Primitive wrapper classes are used to create an that needs to represent primitive types in classes (i.e., in the Java API), in the package and in the
reflection Reflection or reflexion may refer to:
Science and technology
* Reflection (physics), a common wave phenomenon
** Specular reflection, reflection from a smooth surface
*** Mirror image, a reflection in a mirror or in water
** Signal reflection, in ...
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.,
[ 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
Primitive wrapper classes are not the same thing as primitive types. Whereas variables, for example, can be declared in Java as data types double, 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.[
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 class.
The wrapper classes BigDecimal
and BigInteger
are not one of the primitive wrapper classes but are immutable.
Atomic wrapper classes
With Java 5.0, additional wrapper classes were introduced in the package. These classes are mutable and cannot be used as a replacement for the regular wrapper classes. Instead, they provide atomic operation
In concurrent programming, an operation (or set of operations) is linearizable if it consists of an ordered list of invocation and response events (event), that may be extended by adding response events such that:
# The extended list can be re-e ...
s for addition, increment and assignment.
The atomic wrapper classes and their corresponding types are:
:
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
Reference is a relationship between objects in which one object designates, or acts as a means by which to connect to or link to, another object. The first object in this relation is said to ''refer to'' the second object. It is called a '' name'' ...
. (See "Generics in Java
Generics are a facility of generic programming that were added to the Java programming language in 2004 within version J2SE 5.0. They were designed to extend Java's type system to allow "a type or method to operate on objects of various types whil ...
" for a description of type parameters in Java).
V
See also
* java.lang
Java Platform, Standard Edition (Java SE) is a computing platform for development and deployment of portable code for desktop and server environments. Java SE was formerly known as Java 2 Platform, Standard Edition (J2SE).
The platform uses ...
* java.lang.reflect
* Java programming language
Java is a high-level, class-based, object-oriented programming language that is designed to have as few implementation dependencies as possible. It is a general-purpose programming language intended to let programmers ''write once, run anywh ...
* Java syntax
The syntax of Java refers to the set of rules defining how a Java program is written and interpreted.
The syntax is mostly derived from C and C++. Unlike in C++, in Java there are no global functions or variables, but there are data members ...
* Java compiler
A Java compiler is a compiler for the programming language Java. The most common form of output from a Java compiler is Java class files containing platform-neutral Java bytecode, but there are also compilers that output optimized native machine ...
References
{{Reflist
Java (programming language)