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
10
votes
5 answers

Autoboxing Unboxing Operator (!=) and (==) difference

public class T1 { public static void main(String[] args) { // TODO Auto-generated method stub Integer i1 = 1000; Integer i2 = 1000; if(i1 != i2) System.out.println("different objects"); …
Saurabh Sharma
  • 489
  • 5
  • 20
10
votes
5 answers

Why java does not autobox int[] to Integer[]

When I do the following, arrayList1 - contains one element and it is an int[]. arrayList2 - not compiling (Error : The constructor ArrayList(List) is undefined) arrayList3 - contains 7 elements and they are Integer objects Here's…
ironwood
  • 8,936
  • 15
  • 65
  • 114
10
votes
2 answers

Do autoboxing and unboxing behave differently in Java and C#

I am manually converting code from Java (1.6) to C# and finding some difficulty with the behaviour of primitives (int and double). In C# it appears that almost all conversions happen automatically List list1 = new List(); //…
peter.murray.rust
  • 37,407
  • 44
  • 153
  • 217
10
votes
2 answers

Initializing with Character vs char array

This prints false List vowelsList=Arrays.asList(new char[]{'a','e','i','o','u'}); System.out.println(vowelsList.contains('a'));//false This prints true List vowelsList=Arrays.asList(new…
Anirudha
  • 32,393
  • 7
  • 68
  • 89
10
votes
1 answer

Generate warnings for autoboxing use

I would like to generate warnings for ALL autoboxing and unboxing. Has anyone found an effective way? Eclipse catches basic autoboxing errors: eg. Integer i = null; i++. But fails on anything complex, and isn't really what I'm after. I've looked at…
David Lavender
  • 8,021
  • 3
  • 35
  • 55
10
votes
2 answers

Wrapper classes - why integer literals fail for Long but work for anything smaller

Just trying to understand auto-boxing, which I do apart from one thing: Short s = 250; Long l = 250; The assignment to Long l fails. This, I expect, is because you cannot widen then box (i.e. it tries to widen the int value 250 to a long and then…
Neil Walker
  • 6,400
  • 14
  • 57
  • 86
9
votes
2 answers

How do I represent the boxed Double in pure Scala?

In Scala there are 2 representations of double-precision numbers, one is an AnyVal, the other AnyRef. On the JVM they are mapped to the primitive double and the class java.lang.Double respectively. Now what happens on platforms other than the JVM? I…
Duncan McGregor
  • 17,665
  • 12
  • 64
  • 118
9
votes
3 answers

Does having a wrapper object return value (e.g. Integer) cause auto boxing in Java?

I couldn't find a definitive answer for this seemingly simple question. If I write a method like this: public Integer getAnInt() { int[] i = {4}; return i[0]; } is the return value autoboxed into an Integer, or does it depend on what happens to…
L. Blanc
  • 2,150
  • 2
  • 21
  • 31
9
votes
5 answers

Widening and Boxing Java primitives

Widening and Boxing Java primitives. I know it is not possible to widen a wrapper class from one to another as they are not from the same inheritence tree. Why though is it not possible to widen a primitive to another primitive type and autobox the…
ziggy
  • 15,677
  • 67
  • 194
  • 287
9
votes
3 answers

Why doesn't my primitive-type-argumented method override the wrapper-type-argumented super class method?

public class WrapperClasses{ void overloadedMethod(Number N){ System.out.println("Number Class Type"); } void overloadedMethod(Double D){ System.out.println("Double Wrapper Class Type"); } void…
amarnath harish
  • 945
  • 7
  • 24
9
votes
5 answers

Correct way of converting String to Long in Java

What is the most preferred way of converting a String to Long (the Object) in Java. Long a = new Long(str); OR Long a = Long.parseLong(str); Is there a correct way here because both seem to have the same level of readability and is it acceptable…
Humus
  • 170
  • 1
  • 3
  • 10
9
votes
1 answer

Java allows to assign byte to java.lang.Short but not to java.lang.Integer

final byte b = 12; Short s = b; Integer i = b; Program compiles fine for Short but for Integer compilation fails with "incompatible types" message. I am having difficult time trying to understand this behavior. I could not find anything for…
9
votes
2 answers

Equality comparison of `boolean` and `Object` allowed?

The following code public class TestComparison { public static void main(String[] args) throws Exception { boolean b = true; Object o = new Boolean(true); System.out.println("comparison result: "+ (o == b));…
sleske
  • 81,358
  • 34
  • 189
  • 227
9
votes
5 answers

Java Iterator for primitive types

I have a Java class of the following form: class Example { private byte[][] data; public Example(int s) { data = new byte[s][s]; } public byte getter(int x, int y) { return byte[x][y]; } public void setter(int x, int y, byte z) {…
recursion.ninja
  • 5,377
  • 7
  • 46
  • 78
9
votes
2 answers

Unwanted autoboxing magic on Numbers

The following program prints respectively 'false' and 'true': Number n = true ? new Long(1) : new Double(2.0); System.out.println(n instanceof Long); System.out.println(n instanceof Double); So it will not be a Long but a Double. However, it works…
poroszd
  • 592
  • 4
  • 12