Java Wrapper Classes


In java, each of the 8 primitive data type has associated wrapper class. These classes wrap (cover) the value of data type into object of corresponding wrapper class. i.e. primitive data type values can be stored in objects of corresponding wrapper class and vice versa.

Data type Wrapper Class
byte Byte
short Short
int Integer
long Long
float Float
double Double
char Character
boolean Boolean

Wrapper class hierarchy -

Java wrapper classes
As shown in above figure all numeric type inherit Number class . This class define methods to return the value stored in wrapper class of these data types.
byte byteValue();   // return_type  method_Name();
short shortValue();
int intValue();
long longValue();
float floatValue();
double doubleValue();

for example -

Integer a = 10;
int b = a.intValue();   
We can store the value within wrapper class either directly (as in above example) or by using constructor (constructor will be covered later). All numeric primitive type wrapper classes have two types of constructors -
1. One take value of the same type.
Integer(int value)
Double(double value)
For Example -
Integer a=  new Integer(66);
Double b= new Double(10.13);

2. Another take string in valid format.
 Integer("String in valid format")
 Double("String in valid format")
 
For Example -
 Integer a=  new Integer("66");
 Double b= new Double("10.13");
     
 
If string is not in valid format then a NumberFormatException is thrown.
Note -
1. Each constructor can take value in the corresponding data type or string in that format.
2. Float can have float value as well as double value as parameter.

Example -


public class Demo
{
 public static void main(String args[]) 
 {
 Float f1 = new Float(10.12f);  // boxing
 float f2 = f1.floatValue();      // unboxing
   System.out.println(f1 + "  " + f2); 
  }
}
             
      
      
Output -   10.12   10.12
This program wraps the float value 10.12 within a Float wrapper class and this process of wrapping (storing) primitive values in corresponding wrapper class is known as Boxing.

And in the next line value is extracted from Float wrapper class using floatValue() method of Number class and stored in primitive type variable f. This process of extracting value from wrapper class is known as Unboxing.

Character and Boolean wrapper classes -

Character and Boolean are the wrapper class for char and boolean primitives type respectively. These classes directly belong to Object class.
Character class -
Character class has the following constructor -
Character(char ch)
Here ch represents a char value that you want to wrap in Character wrapper class.
You can extract value back using charValue() method.
For example -
     
      
char ch = 'a';  
Character ch  = new Character(ch);
char b = ch.charValue();
Boolean class -
Boolean class has the following constructors -
 
1- Boolean(boolean b1)
    
2- Boolean(String)
 
    
Here b1 represents boolean value and String represents a string in valid format(i.e. "True" or "False") that you want
to wrap in Boolean wrapper class. You can extract value back using booleanValue() method.
        
        
For example -

boolean b1 = true;
String s1 = "true";
Boolean b2 = new Boolean(b1);  // boxing
Boolean b3 = new Boolean(s1);  // boxing
boolean b4 = b2.booleanValue(); //unboxing
boolean b5 = b3.booleanValue(); //unboxing

Autoboxing and AutoUnboxing -

Since jdk1.5 java supports the concept of Autoboxing and AutoUnboxing. The process of automatic conversion of primitive data type values into objects of coresponding wrapper class is known as Autoboxing.

For example -
int a=10;
Integer b = a;
And the process of extracting back the value of primitive data type from object type of coresponding wrapper class is known as AutoUnboxing.
For example -  
Character ch1 = 'a';
char ch2 = ch1;  
So now there is no need to do it manualy, Java automatically does it for you.

Example -


public class Demo
{
 public static void main(String args[]) 
 {
  Float f1 = 10.12f;  // Autoboxing
  float f2 = f1;         // AutoUnboxing
  System.out.println(f1 + "   " + f2); 
 }
}


Output -     10.12    10.12

Converting String into primitive type -

Each of the wrapper classes except Character have a parse() method that is used to convert a valid value of primitive type in string format into corresponding primitive data type.
It is a static method i.e. can be accessed directly by class name without creating any object of the class(static keyword will be covered later in detail). It is defined as follows -
Wrapper Class Parse Method
Byte parseByte("String")
Short parseShort("String")
Integer parseInt("String")
Long parseLong("String")
Float parseFloat("String")
Double parseDouble("String")
Boolean parseBoolean("String")
Note -
Here string should be in a valid format that represent primitive type value. Each of the method return corresponding primitive type value.

Example -


public class Demo
{
 public static void main(String args[]) 
 {
  String s1 = "12";
  String s2 = "12.12f";
  String s3 = "True";
  int a = Integer.parseInt(s1); 
  
  //Note static methods can be accessed directly by class name.
  
  float b = Float.parseFloat(s2);
  boolean c = Boolean.parseBoolean(s3);
  System.out.println("a = "+a+" b = "+b+" c = "+c);
 }
}


Output -    a = 12 b = 12.12 c = true