Home‎ > ‎Applied Core Java‎ > ‎Collection Framework‎ > ‎

Autoboxing

Previously when placing a primitive data into one of the Java Collection Framework we have to wrap it to an object because the collection cannot work with primitive data. Also when calling a method that requires an instance of object than an int or long, than we have to convert it too.

But now, starting from version 1.5 we were offered a new feature in the Java Language, which automate this process, this is call the Autoboxing. When we place an int value into a collection it will be converted into an Integer object behind the scene, on the other we can read the Integer value as an int type. In most way this simplify

Autoboxing is a new feature offered in the Tiger (1.5) release of Java SDK. In short auto boxing is a capability to convert or cast between object wrapper and it's primitive type.

Boxing and Autoboxing


With AutoboxingWithout Autoboxing
int i;
Integer j;
i = 1;
j = 2;
i = j;
j = i;
int i;
Integer j;
i = 1;
j = new Integer(2);
i = j.intValue();
j = new Integer(i);

Before Autoboxing

int i = 5;
Integer iObj = new Integer(i) ;  //Manual boxing
int k  = iObj.intValue(); //Manual unboxing

List l = new List();
l.add( iObj ) ;  // Collection can accept objects only

After Autoboxing

int i = 5;
Integer iObj = i ; //Autoboxing
int k = iObj;  // Auto unboxing

List l = new List();
l.add( i ) ;  // Collection can accept primitive value

Program :


Example 1 - Manual boxing Coding

import java.util.ArrayList;
import java.util.Iterator;

/*
 * A program to implement ManualBoxing.
 *
 */

public class ManualBoxing {
    public static void main(String[] args) {

        ArrayList list = new ArrayList();

        for (int i = 0; i < 10; i++) {
            // Manually covert primitive variable 'i' into Integer object
            list.add(new Integer(i));
        }

        int sum = 0;

        Iterator it = list.iterator();

        while (it.hasNext()) {
            Integer j = (Integer) it.next();

            // Manually get primitive data from Integer object
            sum += j.intValue();
        }

        System.out.println("The sum is " + sum);
    }
}

Step 2 - Deployment

  1. Create a folder 'c:\sunilos'. 
  2. Create or copy ManualBoxing.java  into 'c:\sunilos' folder.
  3. Open your command prompt and go to  'c:\sunilos'
  4. Compile ManualBoxing.java with help of 
    • javac ManualBoxing.java command. Command will create class file in the same folder.
  5. Congratulations!! your Java program is ready to serve.

Step 3 - Testing

  1. Make sure you are on Command Prompt under c:\sunilos directory
  2. Now start your java program from command prompt with help of 
    • java ManualBoxing command.

Output

The sum is 45.

Example 2 - Autoboxing Coding

import java.util.ArrayList;

/*
 * A program to implement AutoBoxing.
 */

public class Autoboxing {
    public static void main(String[] args) {
        ArrayList<Integer> list = new ArrayList<Integer>();
        for (int i = 0; i < 10; i++) {
            // AutoBoxing covert primitive variable 'i' into Integer object
            list.add(i);
        }
        int sum = 0;
        for (Integer j : list) {
            // AutoBoxing get primitive data from Integer object
            sum += j;
        }
        System.out.println("The sum is " + sum);
    }
}

Step 2 - Deployment

  1. Create a folder 'c:\sunilos'. 
  2. Create or copy Autoboxing.java  into 'c:\sunilos' folder.
  3. Open your command prompt and go to  'c:\sunilos'
  4. Compile Autoboxing.java with help of 
    • javac Autoboxing.java command. Command will create class file in the same folder.
  5. Congratulations!! your Java program is ready to serve.

Step 3 - Testing

  1. Make sure you are on Command Prompt under c:\sunilos directory
  2. Now start your java program from command prompt with help of 
    • java Autoboxing command.

Output

The sum is 45.

<<Previous | Next>>
ċ
Autoboxing.java
(1k)
Mahima Hardia,
Aug 19, 2011, 11:34 PM
ċ
ManualBoxing.java
(1k)
Mahima Hardia,
Aug 19, 2011, 11:35 PM
Comments