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
vote
0 answers

JAVA: using comparison of two autoboxed values gives me inconsistent results

To play with autoboxing and understand it better, I developed this piece of code in JAVA: public class Autoboxing { public static void cmp(Integer a, Integer b) { if (a < b) System.out.printf("%d < %d\n", a, b); else if (a == b)…
embiem
  • 113
  • 5
1
vote
1 answer

Concept of ArrayBag

I am having trouble understanding a part of class slide that says: Storing Items in an ArrayBag : We store the items in an array of type Object. public class ArrayBag implements Bag { private Object[] items; private int numItems; .... }…
CMSC
  • 157
  • 10
1
vote
2 answers

Problems converting Integer object to type int

Not sure what is going on here. Seems like an auto-boxing problem but I've been stuck on this for awhile and thought it might benefit me to stop stressing out and get some more experienced hands on this. The assignment is essentially implementing a…
Scot Matson
  • 745
  • 6
  • 23
1
vote
1 answer

Unboxing Integer[] in AsyncTask

I am trying to populate several TextViews with data from AWS using an AsyncTask. In order to the load the data from AWS, I must submit a range-key value which is an int. AsyncTask will only allow you to send Integers as parameters. Problem: How can…
1
vote
3 answers

C#: Why does the following comparison indicate that 0 != 0

I've stumbled across in interesting bug in some comparison code recently where two objects both have a property equal to 0.0m. When that property is converted to an int and compared, the comparison is never equal. Reproduction below: Take an…
LiamK
  • 795
  • 6
  • 16
1
vote
0 answers

Strong synergy between autoboxing and varargs

As said in the java documentation i.e. https://docs.oracle.com/javase/1.5.0/docs/guide/language/varargs.html, There is a strong synergy between autoboxing and varargs, which is illustrated in the following program using reflection: // Simple…
Baji Shaik
  • 245
  • 1
  • 4
  • 11
1
vote
3 answers

How to safely handle Java's wrapped primitives

I'm writing a program which needs to handle objects with many wrapped number variables such as Long, Double, Integer etc. How can I safely perform numeric operations on these without having to put null-checks everywhere? I expect this is something…
JohnEye
  • 6,436
  • 4
  • 41
  • 67
1
vote
4 answers

In Java, is it possible to override methods if return types are respectively a primitive and its wrapper class?

While working with the idea of overriding and overridden methods in Java I have noticed that there is some flexiblity given for the return types of such methods. Here is a bit of theory: "The return type of an overriding method in the derived class…
1
vote
2 answers

Is a @specialized Ordered possible?

I want to implement a "clamp" function for numeric types: Int, Double, Float, etc. (If it works for other things, like Strings, that's fine too, but that is not my goal.) This demonstrates that specialization with an implicit Ordering still…
Paul Draper
  • 78,542
  • 46
  • 206
  • 285
1
vote
3 answers

== operator return false if compare Double type with the same value

Why equal method is false when I compare two double primitive types with the same value? However integer is not public class EqualMethod { public static void main(String[] args) { Double value1 = 6.2; Double value2 =…
user2904961
1
vote
2 answers

Getting Error with java autoboxing

Can anyone tell why the auto boxing is not working, and why with constructor it works fine: int intValue = 12; Double FirstDoubleValue = new Double(intValue); Double SecondDoubleValue = intValue; // ==> Error Thanks for advance :)
Naruto Biju Mode
  • 2,011
  • 3
  • 15
  • 28
1
vote
1 answer

Autoboxing wth ++,-- operator in java

I am confuse about autoboxing unboxing in java. Please see my following two progarm. Integer x = 400; Integer y = x; x++; x--; System.out.println((x==y)); The output is false. I known why the output is false. Because of autoboxing x. Integer x =…
T8Z
  • 691
  • 7
  • 17
1
vote
5 answers

How does comparator work?

I thought the Collections.binarySearch()would return never return a 0 cause the comparison in the comparator is between two Integer which the == operation would always been false, but the run results let me down... Can someone help me out? public…
Adams.H
  • 1,603
  • 4
  • 20
  • 29
1
vote
1 answer

How come autoboxing doesn't work for Short(5)?

How / why is is that autoboxing works for the first example below, but not the second? What's the reasoning / logic behind this? Short i = 5; // works Short i = new Short(5) // doesn't work Short i = new Short( (short) 5) // works again, but uses…
abc32112
  • 2,457
  • 9
  • 37
  • 53
1
vote
2 answers

Autoboxing not working?

I have the following code: static boolean nextPerm(int[] A) { int N = A.length; int k = N - 1; int[] S = { }; while (k >= 0) { if (S.length > 0 && containsLarger(S, A[k])) { int v =…
Leon Helmsley
  • 575
  • 2
  • 6
  • 17