Showing posts with label Wrapper Classes. Show all posts
Showing posts with label Wrapper Classes. Show all posts

Boxing and Unboxing

Boxing and Unboxing

Boxing and Unboxing in java is nothing but conversion from Wrapper classes to primitive datatypes and vice-verse in Java. In our earlier tutorial we have seen about list of Wrapper classes and their corresponding primitive datatypes in Java
In that tutorial already we have seen about 8 wrapper classes and their class hierarchy also. So what is Boxing and Unboxing is when we convert an int to an Integer, a double to a Double etc., is called as Boxing and opposite conversion like Integer to int, Double to double is called as Unboxing. Now lets see simple example of both Boxing and Unboxing in java.



public class BoxingTest {

 public static void main(String[] args) {
 
  int _int = 1;
  byte _byte = 2;
  short _short = 3;
  long _long = 4;
  float _float = 5.5f;
  double _double = 6;
  char _char = 'a';
  boolean _boolean = true;
  
  
  // Boxing (Primitive datatype to Wrapper classes)
  Integer w_int = new Integer(_int); 
  Byte w_byte = new Byte(_byte);
  Short w_short = new Short(_short);
  Long w_long = new Long(_long);
  Float w_float = new Float(_float);
  Double w_double = new Double(_double);
  Character w_char = new Character(_char);
  Boolean w_boolean = new Boolean(_boolean);
 
  
  
  // Unboxing (Wrapper class to Primitive datatypes)
  int _int1 = w_int;
  byte _byte1 = w_byte;
  short _short1 = w_short;
  long _long1 = w_long;
  float _float1 = w_float;
  double _double1 = w_double;
  char _char1 = w_char;
  boolean _boolean1 = w_boolean;
 }
}


In above example we have seen how converting primitive datatype to Wrapper class values is called as Boxing and next same Wrapper class object are stored into Primitive datatype is called as unboxing in java. 


Wrapper classes in Java


We all many know that Java is Object Oriented programming and in the world of Java everything will be considered as an Object. In that case when we see about primitive datatypes in java those are all not under any classes in Java. And also before JDK 1.5 all data structures used to store only Objects. In those cases they have came with Wrapper classes for all 8 primitive datatypes in Java. As a name represents "Wrapper" it wraps around the primitive datatype and gives the Objects. 

How these wrapping are done in Java from primitive to Wrapper class objects?
Wrapping done by using constructor of each classes. Each class will accept different types of primitive datatype or Object as a constructor parameter and those parameters are converted to Objects. All these 8 Wrapper classes comes inside java.lang package and immutable classes. Apart from these 8 Wrapper classes other wrapper classes like BigDecimal and BigInteger are not one of the primitive wrapper classes and are mutable. 

Below are the list of 8 primitive datatypes and their corresponding Wrapper classes along with their constructor parameter types.

Primitive typeWrapper classConstructor Arguments
byteBytebyte or String
shortShortshort or String
intIntegerint or String
longLonglong or String
floatFloatfloat, double or String
doubleDoubledouble or String
charCharacterchar
booleanBooleanboolean or String


Bellow is the Wrapper class hierarchy


Wrapper classes in Java

The Byte, Short, Integer, Long, Float, and Double wrapper classes are all sub-classes of the Number class. Lets see small example for converting primitive datatype to Object and from Object to primitive type. 


public class WrapperTest {
 
 public static void main(String[] args) {
  
  int _int = 1;
  byte _byte = 2;
  short _short = 3;
  long _long = 4;
  float _float = 5.5f;
  double _double = 6;
  char _char = 'a';
  boolean _boolean = true;
  
  Integer w_int = new Integer(_int); 
  Byte w_byte = new Byte(_byte);
  Short w_short = new Short(_short);
  Long w_long = new Long(_long);
  Float w_float = new Float(_float);
  Double w_double = new Double(_double);
  Character w_char = new Character(_char);
  Boolean w_boolean = new Boolean(_boolean);
  
  System.out.println("Printing Wrapper class");
  System.out.println("Integer      : "+w_int);
  System.out.println("Byte         : "+w_byte);
  System.out.println("Short        : "+w_short);
  System.out.println("Long         : "+w_long);
  System.out.println("Float        : "+w_float);
  System.out.println("Double       : "+w_double);
  System.out.println("Character    : "+w_char);
  System.out.println("Boolean      : "+w_boolean);
  
  int _int1 = w_int;
  byte _byte1 = w_byte;
  short _short1 = w_short;
  long _long1 = w_long;
  float _float1 = w_float;
  double _double1 = w_double;
  char _char1 = w_char;
  boolean _boolean1 = w_boolean;
  
  System.out.println("\nPrinting Primitive datatype from Wrapper class");
  System.out.println("int          : "+_int1);
  System.out.println("byte         : "+_byte1);
  System.out.println("short        : "+_short1);
  System.out.println("long         : "+_long1);
  System.out.println("float        : "+_float1);
  System.out.println("double       : "+_double1);
  System.out.println("char         : "+_char1);
  System.out.println("boolean      : "+_boolean1);
 }
}

OUTPUT:


Printing Wrapper class
Integer      : 1
Byte         : 2
Short        : 3
Long         : 4
Float        : 5.5
Double       : 6.0
Character    : a
Boolean      : true

Printing Primitive datatype from Wrapper class
int          : 1
byte         : 2
short        : 3
long         : 4
float        : 5.5
double       : 6.0
char         : a
boolean      : true