The knowledge sharing zone - Guruzon.com

What is autoboxing in java

This tutorial contains the details about Autoboxing and Auto unboxing feature which introduced from Java 5. It also contains the advantages and examples of when and how to use autoboxing in your java program.
reviews
  • In Java 5, A new feature added, known variously as: autoboxing, auto-unboxing, boxing, and unboxing.
  • Boxing and unboxing makes using wrapper classes more convenient.

What is autoboxing in java

  • Autoboxing is the process by which a primitive type is automatically encapsulated (boxed) into its equivalent type wrapper whenever an object of that type is needed.
  • There is no need to explicitly construct an object.

What is auto unboxing in java

  • Auto unboxing is the process by which the value of a boxed object is automatically extracted (unboxed) from a type wrapper when its value is needed.
  • There is no need to call a method such as intValue( ) or doubleValue( ).

Examples of autoboxing in java

  • In the old, pre Java 5 days, if you wanted to make a wrapper, unwrap it, use it, and then rewrap it, you might do something like this:
Integer y = new Integer(567); // make it
int x = y.intValue(); // unwrap it
x++; // use it
y = new Integer(x); // re-wrap it
System.out.println("y = " + y); // print it

Now, with new and improved Java 5 you can say

Integer y = new Integer(567); // make it
y++; // unwrap it, increment it,
// rewrap it
System.out.println("y = " + y); // print it
  • The code appears to be using the postincrement operator on an object reference variable But it's simply a convenience.
  • Behind the scenes, the compiler does the unboxing and reassignment for you.

Remember - wrapper reference variables can be null

  • This means that you have to watch out for code that appears to be doing safe primitive operations, but that could throw a NullPointerException:
class Boxing2 {
   static Integer x;
   public static void main(String [] args) {
      doStuff(x);
   }
   static void doStuff(int z) {
      int z2 = 5;
      System.out.println(z2 + z);
   }
}
  • This code compiles fine, but the JVM throws a NullPointerException when it attempts to invoke doStuff(x), because x doesn’t refer to an Integer object, so there’s no value to unbox.
  • In the case of method overloading, method with primitive type argument would be given priority instead of wrapper or autoboxed type argument to maintain the backword compatibility.

Use of autoboxing - Advantages of autoboxing

  • Autoboxing and Auto unboxing greatly streamlines the coding of several algorithms, removing the tedium of manually boxing and unboxing values.
  • It also helps prevent errors, Moreover, it is very important to generics, which operates only on objects.
  • Autoboxing makes working with the Collections Framework much easier.
  • With autoboxing it is no longer necessary to manually construct an object in order to wrap a primitive type.
  • You need only assign that value to a type-wrapper reference, Java automatically constructs the object for you.
  • For example, here is the modern way to construct an Integer object that has the value 100:
Integer iOb = 100; // autobox an int

FAQ about autoboxing feature of java

How method overloading works with autoboxing and auto unboxing?


Is there any chance of ambiguity error or runtime exception with autoboxing feature?


What are the advantages of using Autoboxing?


 

You would also like to read:

Use of Wrapper classes in java

Comments :

Name :
Email :
Comment :
Verify Text :
capch image, refresh page if not loaded somehow