Questions tagged [autoboxing]

Boxing is the process of using an object to wrap a primitive value so that it can be used as a reference object; extracting a previously-boxed primitive is called unboxing. Auto(un)boxing is a form of "syntactic sugar" where the compiler automatically performs (un)boxing for you, allowing you to use value and referenced types interchangeably.

Autoboxing is a Java term where the Java compiler automatically performs from a primitive value to its corresponding wrapper class. The inverse of autoboxing is called . It is considered a form of boxing in Object-oriented programming.

Examples of autoboxing:

Converting a char to a Character.

Double x = 3.141592 as opposed to Double x = new Double(3.141592). The double literal is converted to the Double wrapper.

ArrayList<Integer> al = new ArrayList<Integer>();
al.add(100); // Autoboxing performed as an int is passed. Compiler converts to Integer.

Link to Oracle Java Documentation about autoboxing and unboxing

398 questions
23
votes
4 answers

How does auto boxing/unboxing work in Java?

Since JDK 5.0, auto boxing/unboxing was introduced in Java. The trick is simple and helpful, but when I started testing different conversions between wrapper classes and primitive types, I get really confused how the concept of auto boxing works in…
Naruto Biju Mode
  • 2,011
  • 3
  • 15
  • 28
22
votes
2 answers

Java Arrays.asList on primitive array type produces unexpected List type

Possible Duplicate: Arrays.asList() not working as it should? Apparently the return type of Arrays.asList(new int[] { 1, 2, 3 }); is List. This seems totally broken to me. Does this have something to do with Java not autoboxing arrays of…
Finbarr
  • 31,350
  • 13
  • 63
  • 94
21
votes
3 answers

Which is better: letting Java do autoboxing or using valueOf()

I am just wondering is there any difference in letting java autobox say an integer: Integer myInteger = 3; // This will call Integer.valueOf() or having your code as Integer myInteger = Integer.valueOf(3); Is there any micro optimization on this?…
Oscar Gomez
  • 18,436
  • 13
  • 85
  • 118
21
votes
3 answers

Autoboxing versus manual boxing in Java

Why is the second piece of code faster? Map map = new HashMap(); for (int i = 0; i < 50000; i++) { for (double j = 0.0; j < 10000; j++) { map.put(i, j); } } Map map=new…
Andrei N
  • 4,716
  • 4
  • 29
  • 29
20
votes
6 answers

Why can the as operator be used with Nullable?

According to the documentation of the as operator, as "is used to perform certain types of conversions between compatible reference types". Since Nullable is actually a value type, I would expect as not to work with it. However, this code compiles…
recursive
  • 83,943
  • 34
  • 151
  • 241
20
votes
4 answers

Why does autoboxing not use valueOf() when invoking via reflection?

To my understanding following code should print "true", but when I run it it prints "false". public class Test { public static boolean testTrue() { return true; } public static void main(String[] args) throws Exception { …
T-Bag
  • 10,916
  • 3
  • 54
  • 118
19
votes
3 answers

Why doesn't autoboxing overrule varargs when using method overloading in Java 7?

We have a class LogManager in our Java project which looks like this: public class LogManager { public void log(Level logLevel, Object... args) { // do something } public void log(Level logLevel, int value, Object... args) { …
19
votes
7 answers

Java question about autoboxing and object equality / identity

public class Main { /** * @param args the command line arguments */ public static void main(String[] args) { // TODO code application logic here int a1 = 1000, a2 = 1000; System.out.println(a1==a2);//=>true…
OOP
  • 199
  • 1
  • 4
18
votes
3 answers

Performance impact of autoboxing

Usually the compiler generates code to perform boxing and unboxing. But what does the compiler, if the boxed values are not needed? Is the (Oracle standard) compiler smart enough to optimize it away? Take a look at this method: public static void…
deamon
  • 89,107
  • 111
  • 320
  • 448
18
votes
5 answers

Why is foo(1,2,3) not passed to varargs method foo(Object... ) as an Integer[]

Please regard the following lines of code: public static void main(String[] args) { foo(1,2,3); System.out.println("-------------------------------------"); foo(new Integer(1), new Integer(2), new Integer(3)); …
ParkerHalo
  • 4,341
  • 9
  • 29
  • 51
17
votes
4 answers

Java 8 autoboxing + generics: different behaviour with variable vs. method

I found a piece of code that after switching from Java 7 to Java 8 stopped compiling. It does not feature any of the new Java 8 stuff like lambda or streams. I narrowed the problematic code down to the following situation: GenericData g =…
Quota
  • 573
  • 3
  • 11
17
votes
4 answers

java: boolean instanceOf Boolean?

I'm a bit confused: I have a function, that takes an Object as argument. But the compiler does not complain if I just pass a primitive and even recognizes a boolean primitive as Boolean Object. Why is that so? public String test(Object value) { …
epegzz
  • 837
  • 2
  • 10
  • 21
17
votes
4 answers

Boxed Primitives and Equivalence

So I was asked this question today. Integer a = 3; Integer b = 2; Integer c = 5; Integer d = a + b; System.out.println(c == d); What will this program print out? It returns true. I answered it will always print out false because of how I…
John Vint
  • 39,695
  • 7
  • 78
  • 108
16
votes
1 answer

Java null to int Conditional Operator issue

Possible Duplicate: Tricky ternary operator in Java - autoboxing We know that int roomCode = null; is not allowed by the compiler. Then why the Code 1 doesn't give a compiler error, when Code 2 does. Code 1: int roomCode = (childCount == 0) ? 100…
ironwood
  • 8,936
  • 15
  • 65
  • 114
16
votes
1 answer

Java SE 11 - New Cases of Type Conversion in Java Language Specification

JLS §5.2 of Java SE 11 contains some new type conversion cases which JLS of Java 8 doesn't have, see item 4 and item 5 in the list: Assignment contexts allow the use of one of the following: an identity conversion a widening primitive conversion a…
Frank Mi
  • 452
  • 3
  • 11
1 2
3
26 27