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

Java: Unboxing values stored in an Object to an unknown type

I am building something like a data flow graph, with nodes and connections that pass data between them. The base class in this case is ValueTarget, which has a previous and a next target to pass data back and forth. Other classes extend this…
Jay
  • 237
  • 2
  • 14
1
vote
2 answers

Java auto boxing

I switched a project I'm working on over to maven and suddenly auto boxing seems to have broken. My IDE (NetBeans) complains on lines such as the one below with the error "Incompatible types" Integer order = 4;
sensae
  • 493
  • 3
  • 12
1
vote
2 answers

When overloading compiler does not prefer primitive to Object only in case of varargs parameter presence

Please, could you help me to understand why compilation of first call of testVargArgsAutoboxingPriority fails? In case of second call compiler is able to select proper method by preferring primitive (first parameter) to Object but after varargs…
1
vote
3 answers

Is unboxing expensive?

Is unboxking expensive that it's better avoiding it? From this java tutorial: public class ValueOfDemo { public static void main(String[] args) { // this program requires two // arguments on the command line if…
Rollerball
  • 12,618
  • 23
  • 92
  • 161
1
vote
6 answers

In auto boxing, is the conversion based on the variable/reference that we assign to or the other way around?

public class TestBox { Integer i; int j; public static void main (String[] args) { TestBox t = new TestBox(); t.go(); } public void go() { j = i; System.out.println(j); …
1
vote
1 answer

Autoboxing is not working for instance variables?

I defined a Double instance variable like this: public class CurrencyActivity extends Activity { private Button convertBtn; private Double SEKrate; .... public void convertCurrency() { .... Double inputNum =…
Michael SM
  • 715
  • 3
  • 11
  • 25
1
vote
1 answer

Autobox TypeDefs in Objective-C (LLVM 4+)

I have this type of Enum with TypeDef: typedef enum { ControlDisplayOptionNone = 0, ControlDisplayOptionOne = 100 } ControlDisplayOption; And I'd like to be able to put them in an array like this: - (NSArray *)displayOptions { return…
Dan Rosenstark
  • 68,471
  • 58
  • 283
  • 421
1
vote
1 answer

Determine if AutoBoxing was done or not

Is there a way in Java to determine if Autoboxing was done or not? For example void functionInt(Integer i) { //Determine if Integer was passed or int was passed. Is it possible? } int i = 1; int ii = new…
vikky.rk
  • 3,989
  • 5
  • 29
  • 32
1
vote
2 answers

Which of the following is true about the second statement? (unboxing & autoboxing)

I have looked all over the internet to try and solve this problem. Can anyone answer this correctly and explain why? Thank you so much! Look at the following code. Integer myNumber; myNumber = 5; Which of the following is true about the second…
Travis
  • 636
  • 8
  • 11
1
vote
6 answers

Java Integer vs. String Autoboxing Design

In Java, I can do the following to succinctly guard against a NullPointerException: if ("myString".equals(someOtherString)) But I cannot do the same with Integers, e.g. if (5.equals(someOtherInteger)) I get a compile-time error. Any ideas on why…
Travis Webb
  • 14,688
  • 7
  • 55
  • 109
1
vote
2 answers

Is casting long to int cheaper than autoboxing long to Long?

I have a bunch of Thingy objects that I'm keeping track of using long ID numbers. Since I have no guarantees about ID sequence, I'm keeping them in a HashMap for random access. However, in Android, they have this very nice class…
Argyle
  • 3,324
  • 25
  • 44
0
votes
2 answers

Behavior of Scala for/comprehension implicit transformation of numerical types?

I'm trying to understand the behavior of Scala for-loop implicit box/unboxing of "numerical" types. Why does the two first fail but not the rest? 1) Fails: scala> for (i:Long <- 0 to 10000000L) {} :19: error: type mismatch;
IODEV
  • 1,706
  • 2
  • 17
  • 20
0
votes
2 answers

Autoboxing rules, what lies under the hood?

I came across a very weird behavior which later found to be a part of java specs. Let me put the relevant code from the said posting Integer a = 1000, b = 1000; System.out.println(a == b); //Prints false Integer c = 100, d = 100; …
Santosh
  • 17,667
  • 4
  • 54
  • 79
0
votes
2 answers

Strange behaviour with Object.intValue()

I am struggling with a problem, which I can't understand why it doesn't work. How do I pass a variable through the double obj and convert to int? Why does it not work in the top code snippet, but it works in the bottom code snippet below the…
UKDataGeek
  • 6,338
  • 9
  • 46
  • 63
0
votes
4 answers

int vs int[] difference - autoboxing inside generics?

See this piece of code: import java.util.*; public class Temp{ public static void main(String[] args){ List list1 = new ArrayList(); //WORKS! List list2 = new ArrayList(); //WORKS! …
John
  • 349
  • 2
  • 10