Saturday, November 1, 2008

Java 5.0 Autoboxing & Unboxing

Autoboxing And Unboxing in Java 5.0:

Autoboxing, introduced in Java 5, is the automatic conversion the Java compiler makes between the primitive (basic) types and their corresponding object wrapper classes (eg, int and Integer, double and Double, etc).

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.

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 the way we code, no need to do an explisit object casting.

Example how it will look like using the Autoboxing feature:

public static void main(String[] args)
{
Map<String, Integer> map = new HashMap<String, Integer>();

// Here we put an int into the Map, and it accepted
// as it will be autoboxed or converted into the wrapper
// of this type, in this case the Integer object.
map.put("Age", 25);

// Here we can just get the value from the map, no need
// to cast it from Integer to int.
int age = map.get("Age");

// Here we simply do the math on the primitive type
// and got the result as an Integer.
Integer newAge = age + 10;
}
Java 5 supports automatic conversion of primitive types (int, float, double etc.) to their object equivalents (Integer, Float, Double,…) in assignments, method and constructor invocations. This conversion is know as autoboxing.

Java 5 also supports automatic unboxing, where wrapper types are automatically converted into their primitive equivalents if needed for assignments or method or constructor invocations.For example:
int inative = 0;

inative = new Integer(5); // auto-unboxing

Integer intObject = 5; // autoboxing
Before J2SE 5.0, we could not put primitive values like int, long, float, double, char etc. into collections as they are not objects. Collections can hold object references, So we were required to use wrapper classes.

Consider the following example: if we want to store an int “a” into vector “vt”, we have to use wrapper class. And if we want to get element stored at position “0” of vector “vt”, we again have to do casting.
int a = 10;

Vector vt = new Vector();

vt.add(new Integer(a));

int n = ((Integer)vt.elementAt(0)).intValue();
J2SE 5.0 has made this easy. If we want to do the same in Java 5, the code will be like:
int a = 10;

Vector <integer> vt = new Vector <integer> ();

vt.add(a);

int n = vt.elementAt(0); </integer></integer>
Before J2SE 5.0, Java had primtive data types with wrappers around them, So we had to convert from one type to another manually
int a = 12;

Integer b = Integer.valueOf(a);

int c = b.intValue();
But now the life is easy. J2SE 1.5’s autoboxing/unboxing removes the pain of manual conversion between primitives and wrappers. Ofcourse, the compiler creates code to implicitly create objects for us.
int a = 12;

Integer b = a;

int c = b;
Auto-Boxing works also in comparisons (==, <, >, etc.). For instance you can compare int with Integer.
int a = 10;

Integer b = 10;

System.out.println(a==b);
Output:
true
Few things to remember when using autoboxing/unboxing is that java compiler actually manages type conversions for us. So boxing and unboxing too many values can make garbage collector go wild. Hence it is not a advisable to use autoboxing and unboxing for scientific computing, or other performance-sensitive numerical code as it will affect the performance to a good extent.Using primitive types will better serve the purpose there.

0 comments:

Blog Widget by LinkWithin

JS-Kit Comments

  © Blogger template Newspaper III by Ourblogtemplates.com 2008

Back to TOP