Wednesday, 10 July 2013

Autoboxing and Unboxing in Java

Hey all, So today we have a very interesting and quick topic to share about.

Its called Autoboxing and Unboxing in JAVA.

What it is ?

Autoboxing - As the name suggest its some sort of stuff done implicitly i.e automatically by Java compiler. So when write a code line like this:

eg 1: Integer a = 10;

Though our inner soul knows that it will work. but have you ever tried to give a second thought, i.e Why it works ?

Here the Autoboxing comes to play. So what Java does at runtime is it automatically convert the primitive type(int here.) into a object of its wrapper class by invoking  Integer.valueOf(int) method. So after auto conversion at runtime it will become:

runtime : Integer a = Integer.valueOf(10);


Another example where it would play more natural usage:

eg 2: 
 
List<Integer> li = new ArrayList<>();
for (int i = 1; i < 50; i += 2)
    li.add(i);
 
at runtime it become:
 
List<Integer> li = new ArrayList<>();
for (int i = 1; i < 50; i += 2)
    li.add(Integer.valueOf(i));
 

Fact File : If any of you is thinking of why didn't this fellow just do something like List<int> instead of List<Integer>, then lemme tell you primitive types are not allowed in Java Generics.


Now comes the big question, When Java compiler does AutoBoxing ?

There are only two situations:

1. When a primitive type is assigned to its corresponding wrapper class variable.
As we did in first example.

2. When a primitive type is passed to a method which expects a wrapper class object type as a parameter.
As we did in second example


Unboxing - Yeah, you guess it right! its the opposite of boxing. Conversion of Wrapper class to the primitive types is called unboxing.
eg 1: int b = a.intValue();  {a is object of Integer class from the above code}

Another natural example:
 
eg 2:
 
public static int sumEven(List<Integer> li) {
    int sum = 0;
    for (Integer i: li)
        if (i % 2 == 0)
            sum += i;
        return sum;
}

As you know  the % and + operators doesn't operate on objects. So why compiler didn't complained about it at compile time ? Its because at runtime they are automatically unboxed by invoking valueOf() method and are converted to primitive types.

runtime:

public static int sumEven(List<Integer> li) {
    int sum = 0;
    for (Integer i : li)
        if (i.intValue() % 2 == 0)
            sum += i.intValue();
        return sum;
}


Now comes the easiest question of you life, When Java compiler does unboxing ?

There are only two situations:

1. When a wrapper class object is assigned to its corresponding primitive type.
As we did in first example.

2. When the wrapper class object is passed as a parameter to a method that expects a value of the corresponding primitive type.
As we did in second example

Thats all Cheers !! Thanks for reading my first blog.

5 comments:

  1. .Everyone wants to get unique place in the IT industry’s for that you need to upgrade your skills, your blog helps me improvise my skill set to get good career, keep sharing your thoughts with us.

    Android Online Training

    ReplyDelete
    Replies
    1. Thanks Sai. Your comment will surely motivate me to write more blogs.

      Delete
  2. Great explanation about unboxing and autoboxing. Its very worthful to me. keep sharing such kind of knowledgeable information. And i am waiting for your next post. thank you..
    Android Training in Chennai | Web Designing Training in Chennai

    ReplyDelete
    Replies
    1. Thanks Melba. Your comment will surely motivate me to write more blogs.

      Delete
  3. And indeed, I’m just always astounded concerning the remarkable things served by you. Some four facts on this page are undeniably the most effective I’ve had.keep it up!!

    android training in chennai

    android online training in chennai

    android training in bangalore

    android training in hyderabad

    android Training in coimbatore

    android training

    android online training

    ReplyDelete