In
object-oriented programming
Object-oriented programming (OOP) is a programming paradigm based on the concept of '' objects''. Objects can contain data (called fields, attributes or properties) and have actions they can perform (called procedures or methods and impl ...
, a wrapper class is a
class
Class, Classes, or The Class may refer to:
Common uses not otherwise categorized
* Class (biology), a taxonomic rank
* Class (knowledge representation), a collection of individuals or objects
* Class (philosophy), an analytical concept used d ...
that
encapsulates types
Type may refer to:
Science and technology Computing
* Typing, producing text via a keyboard, typewriter, etc.
* Data type, collection of values used for computations.
* File type
* TYPE (DOS command), a command to display contents of a file.
* Ty ...
, 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 a ...
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 ...
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 ...
.
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 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 Execution (computing), invocation and response Event (computing), events, that may be extended by adding response events such tha ...
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
A 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 ''nam ...
. (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).
See also
* java.lang
* java.lang.reflect
* Java programming language
Java is a high-level, general-purpose, memory-safe, object-oriented programming language. It is intended to let programmers ''write once, run anywhere'' ( WORA), meaning that compiled Java code can run on all platforms that support Jav ...
* Java syntax
The syntax of Java is the set of rules defining how a Java program is written and interpreted.
The syntax is mostly derived from C and C++. Unlike C++, Java has no global functions or variables, but has data members which are also regarde ...
* Java compiler
A Java compiler is a compiler for the Java programming language.
Some Java compilers output optimized machine code for a particular hardware/operating system combination, called a '' domain specific'' ''computer system''. An example would be the ...
References
{{Reflist
Java (programming language)