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

Generics with autoboxing and unboxing of primitives

Why autoboxing and unboxing of primitives not happens with Generics Java. public static T addNumber(T a , T b) { int c = a*b; System.out.println(c); return c; } Here why * operation can't be performed and why can't return…
Pooja
  • 31
  • 5
-1
votes
4 answers

Return type is not returning as int when we perform unboxing

I am using autoboxing and unboxing in Java. Return type for Autoboxing is fine. no issues. but for unboxing the return type is coming as Integer instead of int. please find my coding on below and please let me know please package…
Ashok
  • 115
  • 3
  • 8
-1
votes
4 answers

Java, Integers not unique but how are they compared in collections?

I used this straight-forward method: Collection aCollection = Arrays.asList(1,2,3,4,5,6); Integer a = new Integer(5); if( aCollection.contains(a) ) System.out.println("aCollection contains 5"); Result is "aCollection…
user2622016
  • 6,060
  • 3
  • 32
  • 53
-2
votes
1 answer

Why final byte variable cannot be converted to Integer during auto-boxing?

There is no error when I try to autobox i2 to Byte,but when I do vise-versa(b1 to Integer),then an error occurs. final byte b1 = 1; Integer i1 = b1; //error final int i2 = 1; Byte b2 = i2;// no error byte…
-2
votes
2 answers

Autoboxing rules

Why does autoboxing happen in the method public static int compareAges(Person p1, Person p2) { return ((Integer) p1.getAge()).compareTo(p2.getAge()); } but we get a compiler error in the method public static int compareAges(Person p1, Person…
displayName
  • 13,888
  • 8
  • 60
  • 75
-3
votes
1 answer

Unboxing Integer cast from null

What is the value of i when you execute the code below and why? int i = (Integer)null;
Gustavo
  • 1,332
  • 2
  • 16
  • 39
-3
votes
1 answer

Parsing or autoboxing or unboxing

So I already read what parsing is in the threads here, but wouldnt this be autoboxing or unboxing because it goes from an int to an Integer? This is the sentence When an integer is added to an array list declared as ArrayList, Java…
piper
  • 23
-5
votes
3 answers

What if(null) statement return in Java?

I have a method which has a Boolean parameter, like: public void method(Boolean parameter){ ... } Can I just use if(parameter) to determine the logic? What if parameter is null?
Al2O3
  • 3,103
  • 4
  • 26
  • 52
1 2 3
26
27