Autoboxing in Java

Filed Under: Java

Autoboxing in java was introduced in Java 1.5. Autoboxing and unboxing is a convenient way to auto transform primitive data type to it’s corresponding java wrapper classes and vice versa.

Autoboxing in Java

Converting a primitive data type into an object of the corresponding wrapper class is called autoboxing. For example, converting int to Integer or converting long to Long object.

Java compiler applies autoboxing when a primitive value is:

  1. Passed as a parameter to a method that expects an object of the corresponding wrapper class. For example a method with Integer argument can be called by passing int, java compiler will do the conversion of int to Integer.
  2. Assigned to a variable of the corresponding wrapper class. For example, assigning a Long object to long variable.

Unboxing in Java

Converting an object of a wrapper type to its corresponding primitive data type is called unboxing.

Java compiler applies unboxing when an object of a wrapper class is:

  1. Passed as a parameter to a method that expects a value of the corresponding primitive type.
  2. Assigned to a variable of the corresponding primitive type.

Java Autoboxing Example

Here is a small java program showing examples of autoboxing and unboxing in java.


package com.journaldev.util;

import java.util.ArrayList;
import java.util.List;

public class AutoboxingUnboxing {

	public static void main(String[] args) {
		int i = 5;
		long j = 105L;

		// passed the int, will get converted to Integer object at Runtime using
		// autoboxing in java
		doSomething(i);

		List<Long> list = new ArrayList<>();

		// java autoboxing to add primitive type in collection classes
		list.add(j);
	}

	private static void doSomething(Integer in) {
		// unboxing in java, at runtime Integer.intValue() is called implicitly to
		// return int
		int j = in;

		// java unboxing, Integer is passed where int is expected
		doPrimitive(in);
	}

	private static void doPrimitive(int i) {

	}
}

Note: It’s not a good idea to rely on autoboxing always, sometimes it can cause compiler error that method is ambiguous when a method is overloaded. Please refer to below screenshot for a quick example.

autoboxing in java

Read more at this StackOverflow article. Also go through java ambiguous method call error.

That’s all about autoboxing and unboxing in java. This feature is very helpful in reducing code size because we don’t have to convert primitive type to object explicitly.

Comments

  1. Paul Weemaes says:

    Hi Pankaj,

    Your statement above on autoboxing: “Java compiler applies autoboxing when a primitive value is assigned to a variable of the corresponding wrapper class. For example, assigning a Long object to long variable.”

    The example (“assigning a Long object to long variable”) suggests autoboxing on the 3rd line of the following example.

    Long wrappedLong = 10L;
    long primitiveLong;
    primitiveLong = wrappedLong ; /* Autoboxing here???? I don’t think so*/

    I think this is not correct, it is a case of autounboxing, since

    primitiveLong = wrappedLong ;

    does in fact

    primitiveLong = wrappedLong.longValue() ; /* returns a primitive long */

    Therefor I think you meant to write: “For example, assigning a long variable to a Long object.”

    I’m a bit reluctant to bother you with this, since I’m not quite an expert on Java programming (understatement) and apparently no other readers ever bothered to point this out to you (suggesting your statements are correct). However, if I’m wrong, there is something fundamentally wrong with my understanding of autoboxing/autounboxing, and in that case I’m hoping you can clear up my confusion 😉

    Best regards and thanks for your attention!

    Paul Weemaes

  2. Milen G says:

    Thank you Pankaj for so much free content!

  3. suresh says:

    sir it was very nice sir i want learn very perfect in java can u give u number sir plz

  4. somashekar says:

    Thank you

  5. David Adkins says:

    The best

  6. srujan says:

    Excellent website it’s awesome.

Leave a Reply

Your email address will not be published. Required fields are marked *

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages