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
16
votes
2 answers

Method overloading with primitives and their wrappers

I am trying to formulate the rules that are being used in the scenarios below. Please explain why I am getting 2 different outputs. Scenario 1 output: I am an object. class Test { public static void main (String[] args) { Test t = new…
16
votes
1 answer

Why are primitive types such as Int erased to Object in Scala?

In Scala, { x: Option[Int] => x } .getClass .getMethod("apply", classOf[Option[_]]) .getGenericParameterTypes returns Array(scala.Option). I'd initially been expecting to see instead Array(scala.Option), but I…
Scott Morrison
  • 3,100
  • 24
  • 39
15
votes
3 answers

Why auto-boxing marked as a warning?

I understand that auto un-boxing should be done with care because the reference that is being un-boxed can be null. Why is auto-boxing marked as warning as well? Are there some pitfalls I am missing here?
Artium
  • 5,147
  • 8
  • 39
  • 60
15
votes
1 answer

Why during autoboxing final long to Byte compilation error happens, but final int to Byte is ok?

There is no error during auto-boxing of constants with int and short types to Byte, but constant with long type do has error. Why? final int i = 3; Byte b = i; // no error final short s = 3; Byte b = s; // no error final long l = 3; Byte b = l;…
15
votes
2 answers

What is the appropriate way to handle warning: "The expression of type x is boxed into x"

I'm not looking to to turn off or ignore the warning as in The expression of type x is boxed into X?. I'd like to know what the correct way to handle/avoid this warning is if one was so inclined.
bn.
  • 7,739
  • 7
  • 39
  • 54
15
votes
1 answer

Comparing Object and int in Java 7

I recently stumbled on a question that made me stop and think... To me, the code below should always trigger an error, but when one of my colleagues asked me why Eclipse didn't show one, I couldn't answer anything. class A { public static void…
rsalmei
  • 3,085
  • 1
  • 14
  • 15
15
votes
3 answers

NullPointerException through auto-boxing-behavior of Java ternary operator

I tripped across a really strange NullPointerException the other day caused by an unexpected type-cast in the ternary operator. Given this (useless exemplary) function: Integer getNumber() { return null; } I was expecting the following two code…
Markus A.
  • 12,349
  • 8
  • 52
  • 116
14
votes
7 answers

Why can Integer and int be used interchangably?

I am confused as to why Integer and int can be used interchangeably in Java even though one is a primitive type and the other is an object? For example: Integer b = 42; int a = b; Or int d = 12; Integer c = d;
Jamal Khan
  • 9,371
  • 6
  • 23
  • 23
14
votes
5 answers

Comparing doubles in Java gives odd results

I really can'get my head around why the following happens: Double d = 0.0; System.out.println(d == 0); // is true System.out.println(d.equals(0)); // is false ?! This however works as expected: Double d = 0.0; System.out.println(d == 0.0); //…
Simeon
  • 7,582
  • 15
  • 64
  • 101
14
votes
3 answers

What is the difference between (Integer)y and new Integer(y) in java?

What is the difference between the following: Integer in = (Integer)y; and Integer in = new Integer(y); I want to convert int type to Integer type and vice versa. Here is my code for doing that: public class CompareToDemo { public static void…
sunny ranjan
  • 143
  • 6
13
votes
9 answers

Autoboxing: So I can write: Integer i = 0; instead of: Integer i = new Integer(0);

Autoboxing seems to come down to the fact that I can write: Integer i = 0; instead of: Integer i = new Integer(0); So, the compiler can automatically convert a primitive to an Object. Is that the idea? Why is this important?
Harry Quince
  • 2,394
  • 3
  • 15
  • 11
13
votes
3 answers

Kotlin boxed Int are not the same

Please help me understand this piece of code in the kotlin docs:- val a: Int = 10000 print(a === a) // Prints 'true' val boxedA: Int? = a val anotherBoxedA: Int? = a print(boxedA === anotherBoxedA) // !!!Prints 'false'!!! Now, I understand that…
kotlinhelp
  • 167
  • 1
  • 6
13
votes
6 answers

Why can't the compiler/JVM just make autoboxing "just work"?

Autoboxing is rather scary. While I fully understand the difference between == and .equals I can't but help have the follow bug the hell out of me: final List foo = Arrays.asList(1, 1000); final List bar = Arrays.asList(1,…
Pyrolistical
  • 27,624
  • 21
  • 81
  • 106
13
votes
5 answers

Boolean references are null

Can anyone explain why this code results in the below output? @Test public void testBooleanArray() { Boolean[] ab = new Boolean[]{a, b}; a = new Boolean(true); b = new Boolean(false); for(Boolean x : ab) { …
StuPointerException
  • 7,117
  • 5
  • 29
  • 54
12
votes
5 answers

Should autoboxing be avoided in Java?

There are instances where a method expects a primitive type double and you pass a Double object as a parameter. Since the compiler unboxes the passed object will this increase memory usage or decrease performance?
simranNarula
  • 641
  • 3
  • 9
  • 14