In this java wrapper class tutorial, you will see definition of wrapper class with examples.
What is a wrapper class in java? Explain with examples.
In Java, a wrapper class is defined as a class in which a primitive value is wrapped up. These primitive wrapper classes are used to represent primitive data type values as objects. The Java platform provides wrapper classes for each of the primitive data types. For example, Integer wrapper class holds primitive ‘int’ data type value. Similarly, Float wrapper class contain ‘float’ primitive values, Character wrapper class holds a ‘char’ type value, and Boolean wrapper class represents ‘boolean’ value.
All of the numeric wrapper classes are subclasses of the abstract class java.lang.Number as shown in the above Wrapper Class Hierarchy diagram. There are four other subclasses of Number that are not shown here. BigDecimal and BigInteger are used for high-precision calculations. AtomicInteger and AtomicLong are used for multi-threaded applications. The remaining two wrapper classes Character & Boolean are direct subclasses of java.lang.Object.
Examples of Java Wrapper Classes:
To describe the application of wrapper classes in java, let us create an Integer wrapper class object with primitive int type value and get back the primitive value from the wrapper object.
int i = 26; // Primitive data type 'int' // Integer Wrapper class instantiation Integer i_Obj = new Integer(i); // Unwrapping primitive data 'int' from wrapper object int i2 = i_Obj.intValue();
List of all Wrapper Classes in java
The following table shows list of different types of wrapper classes in java:
|Primitive data type||Wrapper class|
Why do we need wrapper classes in java?
Do we really need wrapper classes or wrapper data types in java? Can’t we use primitive data types directly instead of wrapper classes? Actually we can use primitive types in many places but not all. In some cases we can’t use primitive values as is, so wrapper classes are needed/required. For example, if we need to store numbers in a collection, we can’t use primitives because collections such as List, Set, and Map need objects as their elements. In such cases you must use wrapper classes.
What is the difference between primitive data types and wrapper classes in java?
The main difference between primitive data types and wrapper classes (wrapper types) in java is that the primitive types can be used as raw data for operations such as arithmetic, logical, etc. and wrapper classes acts as data holders for these primitive data types.
Important note: The primitive data type values will be stored in Stack Memory whereas wrapper class objects (like any other java objects) are stored in Heap Memory.
Use of wrapper class in java
Although, the primary purpose/use of wrapper classes in java is to wrap primitive values, there are few other uses/advantages of wrapper classes. One feature is to use constants defined by the wrapper class, such as MIN_VALUE and MAX_VALUE, that provide the upper and lower bounds of the data type, and the other application is to use wrapper class methods for converting values to and from other primitive data types, for converting to and from strings, and for converting between number systems (decimal, octal, hexadecimal, binary).
Are wrapper classes mutable in java?
A big “No”. Like Strings, wrapper classes in java such as Integer, Float, Long, and Double are also “immutable”. Once created, the numbers wrapped within the objects of wrapper classes in java.lang package can’t be changed. Read Why Strings & Wrapper classes are immutable and final in java for more details.
In this tutorial, i defined wrapper classes, explained use of wrapper classes, different types of wrapper classes (wrapper classes list), need for wrapper classes, difference between primitive data types and wrapper classes, and wrapper classes immutability. For further learning of java wrapper classes, read the java wrapper class tutorial Java Primitive Wrapper Class Tutorial: API Methods and Constructors.
- Is String Mutable in Java? Why Wrapper Classes are Immutable and Final in Java?
- Java Primitive Wrapper Class Tutorial: API Methods and Constructors
- Java Character Wrapper Class Tutorial: API Methods and Constructors
- Java Integer Wrapper Class Tutorial: API Methods and Constructors
- Explain Most Common Exceptions and Errors in Java with Examples
- What is a Java Exception? Explain Exception Handling in Java with Examples Program
- Java 5 (J2SE 5.0/JDK 1.5) New Features with Examples Tutorial 1
- Java 5 (J2SE 5.0/JDK 1.5) New Features with Examples Tutorial 2
- Explain String vs StringBuffer vs StringBuilder with toString() Example
- How to fix “Exception in thread main java.lang” Common Exceptions? – Tutorial 1