Unboxing in java – Programmerbay https://programmerbay.com A Tech Bay for Tech Savvy Mon, 14 Dec 2020 16:42:28 +0000 en-US hourly 1 https://wordpress.org/?v=6.5.5 https://programmerbay.com/wp-content/uploads/2019/09/cropped-without-transparent-32x32.jpg Unboxing in java – Programmerbay https://programmerbay.com 32 32 What are Wrapper Classes in Java ? https://programmerbay.com/what-are-wrapper-classes-in-java/ https://programmerbay.com/what-are-wrapper-classes-in-java/#respond Mon, 14 Sep 2020 17:19:51 +0000 https://programmerbay.com/?p=5105 Java supports a number of primitive data types or built-in types such as int, float, bool etc. variable which is followed by one of these primitive types can only store that corresponding value type.

In other words, if a variable is of float type, then it keeps float values only . Therefore, there is no sense of having methods.

Java provides a way to make a datatype to act as an object and this object keeps only these particular corresponding type of values, allowing these object to have useful methods to work with.

unnamed file
Figure: Java Wrapper Class Hierarchy


The concept of representing a primitive type as an object and the object that belongs to a class is known as Wrapper class.

For example, Integer, Float, Boolean are Wrapper classes. Wrapper classes are subclasses of abstract Number class.

There are some conversion operations among these wrapper classes and primitive type :

  • Boxing
  • Unboxing
  • Autoboxing
  • Autounboxing


1) Boxing: When a primitive type is converted to its corresponding wrapper class’s object explicitly, is known as Boxing.

int number= 6; 
Integer i=Integer.valueOf(number); // Boxing

2) Unboxing: When an object of the wrapper class is converted to primitive type explicitly, is known as Unboxing.

Integer i= new Integer(20); 
int number=i.intValue();// Unboxing

3) Autoboxing: When a primitive type is converted to its corresponding wrapper class’s object, implicitly by the compiler is known as Autoboxing.

int number= 6; 
Integer i=number; // Autoboxing

4) Autounboxing: When an object of the wrapper class is converted to primitive type, implicitly by the compiler is known as Autounboxing.

Integer i= new Integer(20); 
int number=i; // Autounboxing

Java program to demonstrate the working Integer Wrapper Class

Program:

public class WrapperClass
{
  public static void main (String[]args)
  {
    int intVar1 = 8;
    int intVar2 = 20;
    Integer number1 = new Integer (intVar1);
    Integer number2 = new Integer (intVar2);
      System.out.println ("****** byteValue()********");
    // returns byte equivalent value 
      System.out.println ("Getting byte value using bytevalue() = " +
        number1.byteValue ());
      System.out.println ("\n******CompareTo()********");
    // if it returns 0, then the numbers are same. 
    //if it returns negitive number, then calling object would be smaller than other. 
    //if it returns positive number, then calling object would be larger than other. 
      System.out.println ("Comparing two Integer Objects = " +
        number1.compareTo (number2));
      System.out.println ("\n****** doubleValue()********");
    // returns value of double type which is correspoding to integer Object 
      System.out.println ("Double type of this object = " +
        number1.doubleValue ());
      System.out.println ("\n****** equals()********");
    // returns true, if two objects are same, otherwise false 
      System.out.println
      ("Comparig two objects to check whether they are equal or not = " +
       number1.equals (number2));
      System.out.println ("\n****** toString()********");
    // returns String object identical to integer value 
      System.out.println ("String object = " + number1.toString ());
}
    
}

Output:

]]>
https://programmerbay.com/what-are-wrapper-classes-in-java/feed/ 0