Autoboxing Tutorial

In my Primitive Wrapper Classes Tutorial I discussed the basics of how the wrapper classes work.
Quick refresher ...
int i = 41;
Integer ref = new Integer(i);
// box the primitive int type value into an Integer object
i = ref.intValue();
// unbox the int value from the Integer object

Based on the code sample from above, it would become quite tedious manually exchanging values from primitive types to objects and vice-versa. Say hello to the concept of autoboxing and auto-unboxing! Java autoboxing automatically encapsulates primitive data types into object state for us, Java also does the dirty work by auto-unboxing object state into a primitive value. Autoboxing opens a whole new world of possibilities, especially in future concepts that utilize anything related to Generics. Don't worry about Generics yet, I will discuss them when the time is right.
Here is the same thing as above with autoboxing ...
Integer ref = 41; // autobox the primitive int type value into an Integer object
int i = ref;
// auto-unbox a primitive int value from an Integer object

Integer jenny = new Integer("8675309");
// just boxing a String object to an Integer object
jenny++;
// auto-unbox jenny to int type, perform ++, autobox result back into jenny object
int j = jenny;
// auto-unbox jenny to int type, then assign to int j
j *= 2;
jenny *= 2;
// auto-unbox jenny to int type, evaluate result *= 2, autobox result back into jenny object
System.out.println(j + " - " + jenny );

The above example just scratches the surface of all the cool stuff that autoboxing and auto-unboxing can do for us.



Open the command prompt (CMD - see the Getting Started ) and type in the following commands.

C:\Windows\System32>cd \
C:\>md Java
C:\>cd Java
C:\Java>
C:\Java>md Autoboxing
C:\Java>cd Autoboxing
C:\Java\Autoboxing>Notepad Autoboxing.java

Copy and Paste, or type the following code into Notepad and be sure to save the file when you are done.


class Autoboxing {
    public static void main(String args[]) {
        Integer jenny = new Integer("8675309"); // just boxing a String object to an Integer object
        jenny++; // auto-unbox jenny to int type, perform ++, autobox result back into jenny object
        int j = jenny; // auto-unbox jenny to int type, then assign to int j
        j *= 2;
        jenny *= 2; // auto-unbox jenny to int type, evaluate result *= 2, autobox result back into jenny object
        System.out.println(j + " - " + jenny ); 

        Integer int1 = 12;
        int1 = tripleIt(int1);
        System.out.println("int1 = " + int1);

        int int2 = 12;
        int2 = tripleIt(int2);
        System.out.println("int2 = " + int2);
    }

    static int tripleIt(Integer x) {
        return x * 3;
    }
}


Now switch back to the command prompt (CMD) and type in javac Autoboxing.java and press Enter.
Now type in java Autoboxing and press Enter.


C:\Java\Autoboxing>javac Autoboxing.java
C:\Java\Autoboxing>java Autoboxing
17350620 = 17350620
int1 = 36
int2 = 36


Final thoughts

Now that I have introduced you to the concept of autoboxing and auto-unboxing, I will be going into great detail on each individual wrapper class in future tutorials.


Tutorials