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 does the below code returns NullPointerException

The code below throws a NullPointerException but I don't understand why, the object is not null. public class A{ int GetValue() { return (true ? null : 0); } public static void main(String[] args) { A obj= new A(); …
prabhakar Reddy G
  • 1,039
  • 3
  • 12
  • 23
0
votes
1 answer

Why is Integer.parseInt(String str) returns int instead of Integer in Java?

I am asking this question because I have to a situation where I have the following two methods: public T get(Serializable id) and public T get(int id) I have to use the first method in most scenarios and the second method is already deprecated in…
WowBow
  • 7,137
  • 17
  • 65
  • 103
0
votes
0 answers

Inconsistent unboxing technique in java

Below code, package AutoBoxing; /* * When a wrapper type is initialized do you get a new object created? * Answer: It depends!!!! * 1) If you use a constructor then a new instance will be created. * 2) If you use boxing('Integer i = 127'), and…
overexchange
  • 15,768
  • 30
  • 152
  • 347
0
votes
0 answers

Some Confusing in behavior of Widening, Autoboxing VarArgs method Choice made

I'm bit confusing in the way overloading works with widnening, Autoboxing, and Varargs, I tried to understand the concept as much as i can but there is still some cases I can't get the way of generating the output of them. I need to understand the…
0
votes
5 answers

No autoboxing for BigInteger?

While fixing the code for this question, I realized that autoboxing doesn't work for all types. This code compiles: Integer y = 3; But doing the same with BigInteger doesn't compile: BigInteger x = 3; -> "Type mismatch: cannot convert from int to…
mastov
  • 2,942
  • 1
  • 16
  • 33
0
votes
3 answers

Autoboxed when assigned integer or character for the Character Wrapper

why we can assign both int value and a char value to Character Wrapper type. Autoboxing means boxing for the corresponding wrapper but Character is not the corresponding wrapper of int. It is Integer why both of these statements are…
RoHaN
  • 1,356
  • 2
  • 11
  • 21
0
votes
2 answers

Difference between helper classes and wrapper classes for primitive numeric datatypes in java

I was going through some java video tutorials where the tutor references Double, Integer,Byte etc as helper classes for primitives double,int,byte. But they are supposedly wrapper classes which cause autoboxing and unboxing , so i am not able to…
user3676125
  • 21
  • 1
  • 2
0
votes
1 answer

Java Error coming - The method append(Object) in the type LList is not applicable for the arguments (int)

Check this code in which I am making a singly linked list but I have been getting this error since long. Please find the mistake guys. Basically its a problem of autoboxing and unboxing. I am getting the error when I append an integer which has an…
0
votes
2 answers

Why boxed primitives doesn't support all operators?

After watching the Effective Java video I noticed that boxed primitives types support only four of the six comparison operators which <, >, <=, >= and don't support == and !=. My question is why boxed primitives don't support all operators?
TooCool
  • 10,598
  • 15
  • 60
  • 85
0
votes
2 answers

is there comfortable array without autoboxing

I can't find a comfortable array, which does not use autoboxing. I need to take a very careful look at the memory-size and want to use primitive structures like int instead of their object-equivalent like Integer. Using int[], is very un-comfortable…
user51705
  • 15
  • 1
  • 1
  • 4
0
votes
1 answer

Why does this Java code generate an error in one Eclipse project but not the other?

I have the following test class in a file named Sandbox.java: package cas; public final class Sandbox { public static void main(String[] args) { int primitive = 42; Integer boxed = primitive; } } This is something I would…
Resigned June 2023
  • 4,638
  • 3
  • 38
  • 49
0
votes
1 answer

Manipulating Generics through auto/unboxing

public class Generics { public static T increaseBalance (T amount){ //say I want to increase the amount here, put it into finalBalance and return return finalBalance; } public static void main(String[] args){ …
Rushdi Shams
  • 2,423
  • 19
  • 31
0
votes
1 answer

Java ternary Operator NPE autoboxing String

this simple code is throwing NPE i dont understand why? private Boolean isSangByJohnOrPaul() { final String sangBy = "harrison"; final Boolean result = sangBy.equals("lennon")?true :sangBy //throws NPE at this point …
chiperortiz
  • 4,751
  • 9
  • 45
  • 79
0
votes
1 answer

Java 8 Lambda Autoboxing Reduce-Method

I just updated my NetBeans from 8.0 to 8.0.1 and my JDK from 1.8.0 to 1.8.0u20. I guess most likely my problem is caused by the jdk-update. Before the updates I was able to compile this line of code: int sum = myIntegerList.stream().reduce(0, (a, b)…
jbw
  • 5
  • 1
0
votes
1 answer

Java autoboxing performance comparison

// Hideously slow program! Can you spot the object creation? Long sum = 0L; for (long i = 0; i < Integer.MAX_VALUE; i++) { sum += i; } end = System.currentTimeMillis(); System.out.println("Long sum took: " + (end -…
Timuçin
  • 4,653
  • 3
  • 25
  • 34