- A Wrapper class is a class whose object wraps or contains primitive data types.
- When we create an object to a wrapper class, it contains a field and in this field, we can store primitive data types.
- In other words, we can wrap a primitive value into a wrapper class object.
Need of Wrapper Classes
- They convert primitive data types into objects. Objects are needed if we wish to modify the arguments passed into a method (because primitive types are passed by value).
- The classes in java.util package handles only objects and hence wrapper classes help in this case also.
- Data structures in the Collection framework, such as ArrayList and Vector, store only objects (reference types) and not primitive types.
- An object is needed to support synchronization in multithreading.
Primitive Data types and their Corresponding Wrapper class
Autoboxing and Unboxing
Autoboxing: Automatic conversion of primitive types to the object of their corresponding wrapper classes is known as autoboxing. For example – conversion of int to Integer, long to Long, double to Double etc.
Since Java 5, we do not need to use the valueOf() method of wrapper classes to convert the primitive into objects.
Wrapper class Example: Primitive to Wrapper
//Java program to convert primitive into objects
//Autoboxing example of int to Integer
public class WrapperExample1{
public static void main(String args[]){
//Converting int into Integer
int a=20;
Integer i=Integer.valueOf(a);//converting int into Integer explicitly
Integer j=a;//autoboxing, now compiler will write Integer.valueOf(a) internally
System.out.println(a+" "+i+" "+j);
}}
Output:
20 20 20
Unboxing
The automatic conversion of wrapper type into its corresponding primitive type is known as unboxing. It is the reverse process of autoboxing. Since Java 5, we do not need to use the intValue() method of wrapper classes to convert the wrapper type into primitives.
Wrapper class Example: Wrapper to Primitive
//Java program to convert object into primitives
//Unboxing example of Integer to int
public class WrapperExample2{
public static void main(String args[]){
//Converting Integer to int
Integer a=new Integer(3);
int i=a.intValue();//converting Integer to int explicitly
int j=a;//unboxing, now compiler will write a.intValue() internally
System.out.println(a+" "+i+" "+j);
}}
Output:
3 3 3
Java Wrapper classes Example
//Java Program to convert all primitives into its corresponding
//wrapper objects and vice-versa
public class WrapperExample3{
public static void main(String args[]){
byte b=10;
short s=20;
int i=30;
long l=40;
float f=50.0F;
double d=60.0D;
char c='a';
boolean b2=true;
//Autoboxing: Converting primitives into objects
Byte byteobj=b;
Short shortobj=s;
Integer intobj=i;
Long longobj=l;
Float floatobj=f;
Double doubleobj=d;
Character charobj=c;
Boolean boolobj=b2;
//Printing objects
System.out.println("---Printing object values---");
System.out.println("Byte object: "+byteobj);
System.out.println("Short object: "+shortobj);
System.out.println("Integer object: "+intobj);
System.out.println("Long object: "+longobj);
System.out.println("Float object: "+floatobj);
System.out.println("Double object: "+doubleobj);
System.out.println("Character object: "+charobj);
System.out.println("Boolean object: "+boolobj);
//Unboxing: Converting Objects to Primitives
byte bytevalue=byteobj;
short shortvalue=shortobj;
int intvalue=intobj;
long longvalue=longobj;
float floatvalue=floatobj;
double doublevalue=doubleobj;
char charvalue=charobj;
boolean boolvalue=boolobj;
//Printing primitives
System.out.println("---Printing primitive values---");
System.out.println("byte value: "+bytevalue);
System.out.println("short value: "+shortvalue);
System.out.println("int value: "+intvalue);
System.out.println("long value: "+longvalue);
System.out.println("float value: "+floatvalue);
System.out.println("double value: "+doublevalue);
System.out.println("char value: "+charvalue);
System.out.println("boolean value: "+boolvalue);
}}
Output:
---Printing object values---
Byte object: 10
Short object: 20
Integer object: 30
Long object: 40
Float object: 50.0
Double object: 60.0
Character object: a
Boolean object: true
---Printing primitive values---
byte value: 10
short value: 20
int value: 30
long value: 40
float value: 50.0
double value: 60.0
char value: a
boolean value: true
Primitive Wrapper Classes are Immutable in Java
// Java program to demonstrate that prmitive
// wrapper classes are immutable
class Demo
{
public static void main(String[] args)
{
Integer i = new Integer(12);
System.out.println(i);
modify(i);
System.out.println(i);
}
private static void modify(Integer i)
{
i = i + 1;
}
}
Output :
12
12
The parameter i is reference in modify and refers to same object as i in main(), but changes made to i are not reflected in main(), why?
Explanation:
All primitive wrapper classes (Integer, Byte, Long, Float, Double, Character, Boolean and Short) are immutable in Java, so operations like addition and subtraction create a new object and not modify the old.
The below line of code in the modify method is operating on wrapper class Integer, not an int
i = i + 1;
It does the following:
- Unbox i to an int value
- Add 1 to that value
- Box the result into another Integer object
- Assign the resulting Integer to i (thus changing what object i references)
Since object references are passed by value, the action taken in the modify method does not change i that was used as an argument in the call to modify. Thus the main routine still prints 12 after the method returns.
Java.lang.Number Class in Java
- Most of the time, while working with numbers in java, we use primitive data types.
- But, Java also provides various numeric wrapper sub classes under the abstract class Number present in java.lang package.
- There are mainly six sub-classes under Number class.These sub-classes define some useful methods which are used frequently while dealing with numbers.
- These classes “wrap” the primitive data type in a corresponding object. Often, the wrapping is done by the compiler.
- If you use a primitive where an object is expected, the compiler boxes the primitive in its wrapper class for you.
- Similarly, if you use a Number object when a primitive is expected, the compiler unboxes the object for you. This is also called Autoboxing and Unboxing.
Why to use a Number class object over primitive data?
- Constants defined by the number class, such as MIN_VALUE and MAX_VALUE, that provide the upper and lower bounds of the data type are very much useful.
- Number class object can be used as an argument of a method that expects an object (often used when manipulating collections of numbers).
- Class methods can be used for converting values to and from other primitive types, for converting to and from strings, and for converting between number systems (decimal, octal, hexadecimal, binary).
Methods common to all sub classes of Number:
SN | Modifier & Type | Method | Description |
1) | Byte | byteValue() | It converts the given number into a byte type and returns the value of the specified number as a byte. |
2) | abstract double | doubleValue() | It returns the value of the specified number as a double equivalent. |
3) | abstract float | floatValue() | It returns the float equivalent value of the specified Number object. |
4) | abstract int | intValue() | It returns the value of the specified number as an int. |
5) | abstract long | longValue() | It returns the value of the specified number object as long equivalent. |
6) | short | shortValue() | It returns the value of the specified number as a short type after a primitive conversion. |
public class Test
{
public static void main(String[] args)
{
// Creating a Double Class object with value "6.9685"
Double d = new Double("6.9685");
// Converting this Double(Number) object to
// different primitive data types
byte b = d.byteValue();
short s = d.shortValue();
int i = d.intValue();
long l = d.longValue();
float f = d.floatValue();
double d1 = d.doubleValue();
System.out.println("value of d after converting it to byte : " + b);
System.out.println("value of d after converting it to short : " + s);
System.out.println("value of d after converting it to int : " + i);
System.out.println("value of d after converting it to long : " + l);
System.out.println("value of d after converting it to float : " + f);
System.out.println("value of d after converting it to double : " + d1);
}
}
Output:
value of d after converting it to byte : 6
value of d after converting it to short : 6
value of d after converting it to int : 6
value of d after converting it to long : 6
value of d after converting it to float : 6.9685
value of d after converting it to double : 6.9685
Note : While converting, possible loss of precision may occur. For example, as we can see that fraction part(“.9685”) has been left out while converting from Double object to int data type.