Primitive wrapper class in Java: Difference between revisions

Content deleted Content added
Atomic wrapper classes: Fixed wikilink
Adding intentionally blank description, overriding Wikidata description "computer term referring to a Java class in object-oriented programming"
 
(117 intermediate revisions by 86 users not shown)
Line 1:
{{Short description|none}}
A '''primitive wrapper class''' in the [[Java (programming language)|Java programming language]] is one of eight classes provided in the {{Javadoc:SE|package=java.lang|java/lang}} [[Java package|package]] to provide [[object-oriented programming|object]] [[Method (computer science)|methods]] for the eight [[primitive type]]s. All of the primitive wrapper classes in Java are [[immutable object|immutable]]. [[J2SE]] 5.0 introduced [[autoboxing]] of primitive types into their wrapper object, and automatic unboxing of the wrapper objects into their primitive value—the implicit conversion between the wrapper objects and primitive values.
In [[object-oriented programming]], a '''wrapper class''' is a [[class (computer programming)|class]] that [[Encapsulation (computer programming)|encapsulates]] [[data type|types]], so that those types can be used to create [[object (computer science)|object]] [[instance (computer science)|instances]] and [[method (computer programming)|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]]s,<ref>S. J. Chapman, ''Introduction to Java'', Prentice Hall, 1999.</ref> so that these can be used to create instantiated objects with methods in another class or in other classes.<ref name=murach>J. Murach, ''Murach's Java Programming'', 4th Edition, Mike Murach and Associates, Inc., 2011.</ref><ref>J. R. Hubbard, ''Programming with Java'', Schaum's Outline Series/McGraw Hill, 1998.</ref> The primitive wrapper classes are found in the [[Java API]].
 
[[objectPrimitive type|Wrapperwrapper classes]] are used to represent primitive values whencreate an {{Javadoc:SE|java/lang|Object}} isthat required.needs to Therepresent wrapperprimitive classestypes are used extensivelyin with {{Javadoc:SE|java/util|Collection}} classes (i.e., in the Java API), in the {{Javadoc:SE|package=java.util|java/util}} package and with the classes in the {{Javadoc:SE|package=java.lang.reflect|java/lang/reflect}} [[Reflection (computer science)|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.,<ref name=murach/> but arrays store primitive data types while collections actually store objects.
 
The primitive wrapper classes and their corresponding primitive types are:
 
:{| class="wikitable"
! Primitive type !! Wrapper class !! Constructor arguments
|-
| <code>byte</code> || {{Javadoc:SE|java/lang|Byte}} || <code>byte</code> or <code>String </code>
|-
| <code>short</code> || {{Javadoc:SE|java/lang|Short}} || <code>short</code> or <code>String</code>
|-
| <code>int</code> || {{Javadoc:SE|java/lang|Integer}} || <code>int</code> or <code>String</code>
|-
| <code>long</code> || {{Javadoc:SE|java/lang|Long}} || <code>long</code> or <code>String</code>
|-
| <code>float</code> || {{Javadoc:SE|java/lang|Float}} || <code>float</code>, <code>double</code> or <code>String</code>
|-
| <code>double</code> || {{Javadoc:SE|java/lang|Double}} || <code>double</code> or <code>String</code>
|-
| <code>char</code> || {{Javadoc:SE|java/lang|Character}} || <code>char</code>
|-
| <code>boolean</code> || {{Javadoc:SE|java/lang|Boolean}} || <code>boolean</code> or <code>String</code>
|}
 
==The difference between wrapper classes and primitive types==
The <code>Byte</code>, <code>Short</code>, <code>Integer</code>, <code>Long</code>, <code>Float</code>, and <code>Double</code> wrapper classes are all [[subclasses]] of the {{Javadoc:SE|java/lang|Number}} class.
 
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.<ref name=murach/>
== Void ==
 
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.
Although it is not a wrapper class, the {{Javadoc:SE|java/lang|Void}} class is similar in that it provides an object representation of the [[void type|<code>void</code>]] return type. The <code>Void</code> class is an uninstantiable placeholder class used by the [[Java Platform, Standard Edition#java.lang.reflect|java.lang.reflect]] [[application programming interface|API]] to hold a reference to the {{Javadoc:SE|java/lang|Class}} object representing the [[Java keyword]] <code>void</code>.
 
The <code>Byte</code>, <code>Short</code>, <code>Integer</code>, <code>Long</code>, <code>Float</code>, and <code>Double</code> wrapper classes are all [[subclassessubclass (computer science)|subclass]]es of the {{Javadoc:SE|java/lang|Number}} class.
 
The wrapper classes <code>BigDecimal</code> and <code>BigInteger</code> are not one of the primitive wrapper classes but are immutable.<ref>{{cite web
| url = http://www.javaranch.com/journal/2003/04/immutable.htm
| title = Mutable and Immutable Objects: Which classes are Immutable?
| author = David O'Meara
| date = April 2003
| publisher = Java Ranch
| accessdate = 2012-05-14
| quote = The classes java.math.BigInteger and BigDecimal are not immutable either, although maybe they should have been.
}}</ref>
<ref>{{cite web
| url = http://docs.oracle.com/javase/7/docs/api/java/math/BigInteger.html
| title = Java documentation from oracle
| author = Oracle
| quote = Immutable arbitrary-precision integers.
}}</ref>
 
== Atomic wrapper classes ==
 
With Java 5.0, additional wrapper classes were introduced in the {{Javadoc:SE|package=java.util.concurrent.atomic|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 operation]]s for addition, increment and assignment.
 
The atomic wrapper classes and their corresponding primitive types are:
 
:{| class="wikitable"
Line 49 ⟶ 68:
|}
 
The <code>AtomicInteger</code> and <code>AtomicLong</code> classes are subclasses of the <code>Number</code> class. The <code>AtomicReference</code> class accepts the [[TypeParameter|type parameter]] <code>V</code> that specifies the type of the object [[reference (computer science)|reference]]. (See "[[Generics in Java]]" for a description of type parameters in Java.) Note that while an object type is not a primitive type, an object reference is.
 
== See also ==
Line 55 ⟶ 74:
* [[Java Platform, Standard Edition#java.lang|java.lang]]
* [[Java Platform, Standard Edition#java.lang.reflect|java.lang.reflect]]
* [[Java (programming language)|Java programming language]]
* [[Java syntax]]
* [[Java compiler]]
 
== References ==
{{Reflist}}
 
<!--Categories-->
[[Category:Java (programming language)]]