Autoboxing And Unboxing In Java

In this article we discuss Autoboxing and Unboxing in Java as a Java5 new feature.

Introduction

In this article we discuss Autoboxing and Unboxing in Java as a Java5 new feature.

Java Autoboxing and Unboxing

Conversion of primitive data types into their equivalent Wrapper type automatically is known as Autoboxing and the reverse operation of that is known as Unboxing. This is a new feature of Java5. So the Java programmer doesn't need to write the conversion code.

Advantages

There is no need for manual conversion so we have less coding to do.

Example 1

This example shows Autoboxing in Java:

class AutoBoxingEx1

  {

    public static void main(String args[])

      {

        int x=100;

        //Boxing

        Integer x2=new Integer(x);

        //Boxing

        Integer x3=5;

        System.out.println(x2+" " + x3);

      }

  }

Output

Fig-1.jpg

Using comparison operators to define Autoboxing and Unboxing

Using comparison operators Autoboxing is performed.

Example 2

In this example; using comparison operators we saw Autoboxing and Unboxing .

class UnBoxingEx1

  {

    public static void main(String args[])

      {

        Integer x=new Integer(150);

        //internally unboxing

        if(x<200)

          {

            System.out.println(x);

          }

      }

  }

Output

Fig-2.jpg

Using method overloading define Autoboxing and Unboxing

There are some rules for method overloading with boxing; they are:

  • widening beats varargs
  • widening beats boxing
  • Boxing beats varargs

1. Autoboxing where widening beats varargs

If there is the possibilty of widening and varargs in Java then widening beats var-args.

Example

class AutoboxingEx3

{

static void mthd(int x, int x2)

{

System.out.println("display int display int");

}

static void mthd(Integer... x)

{

System.out.println("display Integer...");

}

public static void main(String args[])

{

short srt1=120, srt2=140;

mthd(srt1, srt2);

}

}

Output

Fig-4.jpg

2. Autoboxing where widening beats boxing

The following example shows the following Autoboxing where widening beats boxing.

Example

class AutoboxingEx2

  {

    static void mthd(int x)

      {

        System.out.println("display int");

      }

    static void mthd(Integer x)

      {

        System.out.println("display Integer");

      }

    public static void main(String args[])

      {

        short srt=120;

        mthd(srt);

      }

  }

Output

Fig-3.jpg

3. Autoboxing where boxing beats varargs

The following example shows that boxing beats a variable argument.

Example

class AutoboxingEx4

  {

    static void mthd(Integer x)

      {

        System.out.println("Display Integer");

      }

    static void mthd(Integer... x)

      {

        System.out.println("Display Integer...");

      }

    public static void main(String args[])

      {

        int k=130;

        mthd(k);

      }

  }

Output

Fig-5.jpg

Method overloading using Boxing and Widening.

The following example shows that Widening and Boxing can't be performed.

Example

class AutoboxingEx5

  {

    static void mthd(Long lng)

      {

        System.out.println("Display Long");

      }

    public static void main(String args[])

      {

        int x=120;

        mthd(x);

      }

  }

Output

Since they generate a compile time error that shows we can't perform Widening and Boxing at a time.

Fig-6.jpg