Autoboxing And Unboxing in Java 5

23 April 2007

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.

Related Posts:

  • No related posts
del.icio.us:Autoboxing And Unboxing in Java 5  digg:Autoboxing And Unboxing in Java 5  spurl:Autoboxing And Unboxing in Java 5  wists:Autoboxing And Unboxing in Java 5  simpy:Autoboxing And Unboxing in Java 5  newsvine:Autoboxing And Unboxing in Java 5  blinklist:Autoboxing And Unboxing in Java 5  furl:Autoboxing And Unboxing in Java 5  reddit:Autoboxing And Unboxing in Java 5  fark:Autoboxing And Unboxing in Java 5  blogmarks:Autoboxing And Unboxing in Java 5  Y!:Autoboxing And Unboxing in Java 5  smarking:Autoboxing And Unboxing in Java 5  magnolia:Autoboxing And Unboxing in Java 5  segnalo:Autoboxing And Unboxing in Java 5  gifttagging:Autoboxing And Unboxing in Java 5

Top Of Page | Trackback

If you found this page useful, consider linking to it. Simply copy and paste the code below into your web site.

It will look like this: Autoboxing And Unboxing in Java 5

2 Responses to “Autoboxing And Unboxing in Java 5”

  1. Anjan Bacchu Says:

    hi there,

    nice post.

    “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”

    If the compiler were doing the task for us, why would the GC get involved ?

    Thank you,

    BR,
    ~A

  2. editor Says:

    Thanks for the comment. You might note that autoboxing will create objects ,other than the natives in the earlier case. Whenever there are objects we cannot ignore Garbage Collector. Look at an example given below. It reveals what I tried to say.

    public class TestAutobox {
    	public int test;
    	//fields are public to skip compiler optimizations
     
    	public void autoboxmethod(Integer i) {
    		test = i;
    	}
     
    	public void nativemethod(int i) {
    		test = i;
    	}
     
    	public static void main(String[] args) {
    		TestAutobox autobox = new TestAutobox();
     
    		long startTime = System.currentTimeMillis();
     
    		for (int i = 0; i < Integer.MAX_VALUE; i++)
    			autobox.nativemethod(i);
     
    		long endTime = System.currentTimeMillis();
    		long result = endTime - startTime;
     
    		System.out.println("native ++++++++++++++++");
    		System.out.println("startTime : " + startTime);
    		System.out.println("endTime : " + endTime);
    		System.out.println("result : " + result);
     
    		startTime = System.currentTimeMillis();
     
    		for (int i = 0; i < Integer.MAX_VALUE; i++)
    			autobox.autoboxmethod(i);
     
    		endTime = System.currentTimeMillis();
    		result = endTime - startTime;
     
    		System.out.println("autobox ++++++++++++++++");
    		System.out.println("startTime : " + startTime);
    		System.out.println("endTime : " + endTime);
    		System.out.println("result : " + result);
     
    	}
     
    }
    OutPut is 
     
    native ++++++++++++++++
    startTime : 1177645700505
    endTime : 1177645715512
    result : 15007
    autobox ++++++++++++++++
    startTime : 1177645715512
    endTime : 1177645830339
    result : 114827

    The performance of Autoboxing depeds on the old trade-off between memory and speed. Autoboxed values are frequently cached and reused. This appeals logical as the wrapper objects are immutable. Look how the above programm outpur changes when I pass a constant.

    native ++++++++++++++++
    startTime : 1177646390041
    endTime : 1177646409836
    result : 19795
    autobox ++++++++++++++++
    startTime : 1177646409836
    endTime : 1177646483650
    result : 73814

Leave a Reply