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
0
votes
1 answer

Why BigDecimal.Zero exist in Java where as Double.zero doesn't

I am curious to understand why do we see BigDecimal.Zero but not Double.Zero, so if we need to assign a double variable as Zero then we need to cast it to (double) 0. Any specific reasons creators added a special zero variable for bigdecimal.
Raj Saraogi
  • 1,780
  • 1
  • 13
  • 21
0
votes
0 answers

ArrayStoreException at runtime in JAVA-8 Stream

I have got some issues in below codelines written in java 8 List> list = Arrays.asList(Arrays.asList(1, 3), Arrays.asList(1, 2), Arrays.asList(1, 2)); Integer[][] arr1 = list.stream().map(l…
Manojak
  • 149
  • 2
  • 3
  • 10
0
votes
0 answers

Wrapper classes autoboxing assignments

Byte b = 10; // Valid Long l = 10; // Invalid Why is it valid in first case but invalid in second case? Best regards
0
votes
1 answer

When autoboxing, does a primitve type array autobox to an array of the Wrapper class for the primitve type?

Although I've taken 2 university classes in Java in the past, I don't recall this. I've researched this online, but could only find references to the associated class type for each primitive type, and no specific indication of what happens to…
Pål Hart
  • 19
  • 6
0
votes
0 answers

Why autobox caching

I can’t understand why in java there is a special array in the wrapper class, for Example Integer, where wrapper objects for primitives are stored: from -128 to 127. Why a new wrapper object not created for this range, like for other primitives?…
Grey may
  • 3
  • 4
0
votes
6 answers

NullPointerException when assigning numbers

There is a strange thing happening when I execute the following code: private void doStuff(Long inLong) { long theNumber = inLong; /* ... */ } Sometimes I see a NullPointerException in the logs at the assignment line and I can't understand…
0
votes
1 answer

Numeric promotion and equality?

Possible Duplicate: Wrapper class and == operator I have a puzzle from my friend. Here is it: public class Test{ public static void main(String[] args){ Integer i = 1000; //10 Integer y = 1000; //10 …
Hung Tran
  • 1,595
  • 2
  • 17
  • 17
0
votes
0 answers

Why do Java generics work for Array of primitives but not primitive?

ArrayList list1 = new ArrayList(); // Error ArrayList list2 = new ArrayList(); // Works fine Generics do not work for primitive types but work fine with Array of primitives. Why? Follow up: The only reason I can think of it…
Surya Saini
  • 1
  • 1
  • 2
0
votes
1 answer

How to count the number of boxing and unboxing operations

I have a loop like this int length = 1000000000; Integer sum1 = 0; for (Integer i = 0; i < length; i++) { sum1 = sum1 + 1; } System.out.println(sum1); How do I count the number of boxing and…
A1122
  • 1,324
  • 3
  • 15
  • 35
0
votes
2 answers

how to get equality behavior with autoboxing comparing with Object and int as Java 7 with Java8

We have code that needs to upgrade from Java 7 to Java 8. Lot's of snippets like this: public class TestJava8 { private static void TestIntegerConversion(){ Map m = new HashMap<>(); …
armnotstrong
  • 8,605
  • 16
  • 65
  • 130
0
votes
1 answer

Is auto boxing in JavaScript temporarily or permanently?

let a = 1; a.toString() When a.toString(), a is auto boxing, will become a Number(1), after a.toString(), will a be unboxing or not?
CodeAlien
  • 766
  • 2
  • 7
  • 15
0
votes
1 answer

Should signature of equals be equals(x: Any) or equals(x: AnyRef)

If the equals method in Scala is supposed to implement the original Java boolean Object.equals(Object x) method, I think it should be written def equals(that: AnyRef): Boolean. IntelliJ generates instead def equals(that: Any): Boolean. Online I've…
ragazzojp
  • 477
  • 3
  • 14
0
votes
0 answers

How to generate warnings for Autoboxing in Kotlin?

In Java, I am able to activate compiler warnings (even errors) for autoboxing. How can I achieve the same in Kotlin? Right now I often find myself decompiling the Kotlin bytecode to Java to double-check, which seems like a hassle. I quickly skimmed…
ndr
  • 21
  • 1
  • 2
0
votes
0 answers

Autoboxing and type detection in Java generics

I have following code: List ints = Arrays.asList(1,2,3,4); asList, defined in java.util.Arrays is as: public static List asList(T... a) { return new ArrayList<>(a); } And AaaryList constructor is defined as: ArrayList(E[]…
Mandroid
  • 6,200
  • 12
  • 64
  • 134
0
votes
1 answer

Why do Scala for loops with a yield autobox ints when converting to a collection?

When using range in Scala 2.12 and proceeding to iterate over the elements why do ints get boxed into java.lang.Integer when they are yielded? The below code allocates 10,000 Integers on the heap. val boxedSeq = for (i <- 1 to 10000) yield…
jhopp994
  • 9
  • 1