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
564
votes
21 answers

How to convert int[] into List in Java?

How do I convert int[] into List in Java? Of course, I'm interested in any other answer than doing it in a loop, item by item. But if there's no other answer, I'll pick that one as the best to show the fact that this functionality is not…
Pablo Fernandez
  • 279,434
  • 135
  • 377
  • 622
300
votes
10 answers

How can I properly compare two Integers in Java?

I know that if you compare a boxed primitive Integer with a constant such as: Integer a = 4; if (a < 5) a will automatically be unboxed and the comparison will work. However, what happens when you are comparing two boxed Integers and want to…
Samatha84
190
votes
8 answers

Returning null as an int permitted with ternary operator but not if statement

Let's look at the simple Java code in the following snippet: public class Main { private int temp() { return true ? null : 0; // No compiler error - the compiler allows a return value of null // in a method signature…
Lion
  • 18,729
  • 22
  • 80
  • 110
183
votes
21 answers

Why do people still use primitive types in Java?

Since Java 5, we've had boxing/unboxing of primitive types so that int is wrapped to be java.lang.Integer, and so and and so forth. I see a lot of new Java projects lately (that definitely require a JRE of at least version 5, if not 6) that are…
Naftuli Kay
  • 87,710
  • 93
  • 269
  • 411
137
votes
4 answers

Booleans, conditional operators and autoboxing

Why does this throw NullPointerException public static void main(String[] args) throws Exception { Boolean b = true ? returnsNull() : false; // NPE on this line. System.out.println(b); } public static Boolean returnsNull() { return…
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
136
votes
12 answers

Weird Integer boxing in Java

I just saw code similar to this: public class Scratch { public static void main(String[] args) { Integer a = 1000, b = 1000; System.out.println(a == b); Integer c = 100, d = 100; System.out.println(c == d); …
Joel
  • 16,474
  • 17
  • 72
  • 93
116
votes
4 answers

Comparing boxed Long values 127 and 128

I want to compare two Long objects values using if conditions. When these values are less than 128, the if condition works properly, but when they are greater than or equal to 128, comparison fails. Example: Long num1 = 127; Long num2 = 127; if…
Viraj Dhamal
  • 5,165
  • 10
  • 32
  • 41
112
votes
3 answers

Why does int num = Integer.getInteger("123") throw NullPointerException?

The following code throws NullPointerException: int num = Integer.getInteger("123"); Is my compiler invoking getInteger on null since it's static? That doesn't make any sense! What's happening?
user282886
  • 3,125
  • 8
  • 22
  • 11
96
votes
10 answers

Why do we use autoboxing and unboxing in Java?

Autoboxing is the automatic conversion that the Java compiler makes between the primitive types and their corresponding object wrapper classes. For example, converting an int to an Integer, a double to a Double, and so on. If the conversion…
user1968030
77
votes
3 answers

When using == for a primitive and a boxed value, is autoboxing done, or is unboxing done

The following code compiles (with Java 8): Integer i1 = 1000; int i2 = 1000; boolean compared = (i1 == i2); But what does it do? Unbox i1: boolean compared = (i1.intvalue() == i2); or box i2: boolean compared = (i1 == new Integer(i2)); So does it…
Thirler
  • 20,239
  • 14
  • 63
  • 92
72
votes
8 answers

How do I convert Double[] to double[]?

I'm implementing an interface that has functionality similar to a table that can contain an types of objects. The interface specifies the following function: double[] getDoubles(int columnIndex); Where I'm stumped is that in my implementation, I'm…
Brian
  • 2,375
  • 4
  • 24
  • 35
69
votes
6 answers

What does it mean to say a type is "boxed"?

I have heard of types being referred to as "boxed" in some languages. In Java, I have heard of "autoboxing". What is this? Is it having wrapper classes for a type? How would my code change if I'm working with boxed or unboxed types?
Nick Heiner
  • 119,074
  • 188
  • 476
  • 699
53
votes
8 answers

Why doesn't Java autoboxing extend to method invocations of methods of the autoboxed types?

I want to convert a primitive to a string, and I tried: myInt.toString(); This fails with the error: int cannot be dereferenced Now, I get that primitives are not reference types (ie, not an Object) and so cannot have methods. However, Java 5…
Mike Stone
  • 44,224
  • 30
  • 113
  • 140
53
votes
3 answers

How can "a <= b && b <= a && a != b" be true?

Here is the code i have to figure it out how is it possible. I have a clue but i do not know how to do it. I think it is about negative and positive numbers and maybe the variable modifiers as well. I am a beginner i looked the solution everywhere…
Adam
  • 279
  • 3
  • 11
52
votes
4 answers

Does autoboxing call valueOf()?

I'm trying to determine whether the following statements are guaranteed to be true: ((Boolean)true) == Boolean.TRUE ((Boolean)true) == Boolean.valueOf(true) ((Integer)1) == Integer.valueOf(1) I've always assumed that autoboxing was equivalent to…
shmosel
  • 49,289
  • 6
  • 73
  • 138
1
2 3
26 27