Wrapper classes in java...

in #study8 years ago

A Wrapper class is a class whose object wraps or contains a primitive data types. When we create an object to a wrapper class, it contains a field and in this field, we can store a primitive data types. In other words, we can wrap a primitive value into a wrapper class object.
OOP_WrapperClass.png

                                 Need of Wrapper Classes
  1. 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).

  2. The classes in java.util package handles only objects and hence wrapper classes help in this case also.

  3. Data structures in the Collection framework, such as ArrayList and Vector, store only objects (reference types) and not primitive types.

  4. An object is needed to support synchronization in multi threading.

                                                              Auto boxing 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.
Example:

// Java program to demonstrate Autoboxing

import java.util.ArrayList;
class Autoboxing
{
public static void main(String[] args)
{
char ch = 'a';;

    // Autoboxing- primitive to Character object conversion
    Character a = ch;

    ArrayList<Integer> arrayList = new ArrayList<Integer>();

    // Autoboxing because ArrayList stores only objects
    arrayList.add(25);

    // printing the values from object
    System.out.println(arrayList.get(0));
}

}

Output:

25

Unboxing: It is just the reverse process of autoboxing. Automatically converting an object of a wrapper class to its corresponding primitive type is known as unboxing. For example – conversion of Integer to int, Long to long, Double to double etc.

// Java program to demonstrate Unboxing
import java.util.ArrayList;

class Unboxing
{
public static void main(String[] args)
{
Character ch = 'a';

    // unboxing - Character object to primitive conversion
    char a = ch;

    ArrayList<Integer> arrayList = new ArrayList<Integer>();
    arrayList.add(24);

    // unboxing because get method returns an Integer object
    int num = arrayList.get(0);

    // printing the values from primitive data types
    System.out.println(num);
}

}

Output:

24

Coin Marketplace

STEEM 0.09
TRX 0.31
JST 0.031
BTC 111448.58
ETH 3830.89
USDT 1.00
SBD 0.62