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

How to spot boxing/unboxing in Scala

Following a suggestion by extempore recently about how to get scala to tell me whether there was boxing going on by looking at the bytecode, I created this class: class X { def foo(ls : Array[Long]) = ls map (_.toDouble) Had a look at the bytecode…
oxbow_lakes
  • 133,303
  • 56
  • 317
  • 449
12
votes
4 answers

Java autoboxing rules

I am a java novice and so confused by the following example. Is it okay to think that "==" sign will compare the values between Integers and "autoboxed" Integers from int, and compare reference address between Integers? What about doubles and…
lkahtz
  • 4,706
  • 8
  • 46
  • 72
12
votes
1 answer

Implementing a fixed size, immutable, and specialized vector

For performance and safety I would like to implement a fixed-size vector which is both immutable and specialized (I need fast arithmetics). My first idea was to use the @specialized annotation (because I need both integers and reals). Here is a…
paradigmatic
  • 40,153
  • 18
  • 88
  • 147
11
votes
6 answers

How does the Java Boolean wrapper class get instantiated?

In java, I can write code like this Boolean b = true ; And it will work. I now have an object that holds the value "true". How does that work? Why don't I have to pass the value through a constructor? Like so: Boolean b = new Boolean( true )…
CrazyPenguin
  • 619
  • 2
  • 10
  • 29
11
votes
2 answers

Do C# generics prevent autoboxing of structs in this case?

Usually, treating a struct S as an interface I will trigger autoboxing of the struct, which can have impacts on performance if done often. However, if I write a generic method taking a type parameter T : I and call it with an S, then will the…
gexicide
  • 38,535
  • 21
  • 92
  • 152
11
votes
4 answers

Java automatic unboxing - is there a compiler warning?

I am a big fan of auto-boxing in Java as it saves a lot of ugly boiler plate code. However I have found auto-unboxing to be confusing in some circumstances where the Number object may be null. Is there any way to detect where auto-unboxing is…
Russ Hayward
  • 5,617
  • 2
  • 25
  • 30
11
votes
3 answers

A boxed value is unboxed and then immediately reboxed

I am getting the Findugs error "A boxed value is unboxed and then immediately reboxed". This is the Code: Employee emp = new Employee() Long lmt = 123L; emp.setLimit(Long.valueOf(lmt)); In this, Employee limit field is of type Long. Could you…
Srinivasan
  • 11,718
  • 30
  • 64
  • 92
11
votes
3 answers

Is Eclipse Juno wrong with this ambiguous method error?

Today I've been playing around with Eclipse Juno. Coming from Helios it is a great upgrade. Everything is working fine, except one new compile error. We are using the java.net framework 'Fuse' and we call the following…
Roy van Rijn
  • 840
  • 7
  • 17
10
votes
2 answers

Avoiding boxing by passing in single element primitive array

I'm working with an interface that takes type Object as its input. This is unfortunate for me as I have primitive data that I sometimes need to pass in through the interface. This of course forces me to box. Profiling has shown this area to be a…
user321605
  • 846
  • 2
  • 7
  • 20
10
votes
1 answer

The expression of type x is boxed into X?

I'm a little confused by a warning that my Eclipse IDE is currently writing next to every expression where types are autoboxed or autounboxed: The expression of type x is boxed into X The expression of type X is unboxed into x Is this a warning I…
B.E.
  • 5,080
  • 4
  • 35
  • 43
10
votes
1 answer

Wrappers and Auto-boxing

There is the following code: Integer time = 12; Double lateTime = 12.30; Boolean late = false; Double result = late ? lateTime : time; //Why here can I assign an Integer to a Double? System.out.println(result); It prints: 12.0 This one doesn't…
Davide
  • 1,931
  • 2
  • 19
  • 39
10
votes
3 answers

Initializing a Double object with a primitive double value

What is happening when a java.lang.Double object is initialized without using a call to the constructor but instead using a primitive? It appears to work but I'm not quite sure why. Is there some kind of implicit conversion going on with the…
Doug Porter
  • 7,721
  • 4
  • 40
  • 55
10
votes
2 answers

How to use (primitive) autoboxing/widening with Hamcrest?

I came across https://code.google.com/p/hamcrest/issues/detail?id=130 to add some sugar syntax for Hamcrest matchers. But the idea was rejected by the Hamcrest developers. Any other smart ideas to make tests better readable by avoiding having to…
Marcel Overdijk
  • 11,041
  • 17
  • 71
  • 110
10
votes
4 answers

Difference between long.Class and Long.TYPE

Do they both return the same thing i.e Long Class. Actually i was using this within PrivilegedAccessor to pass as following PrivilegedAccessor.invokeMethod(MyClass, "MyMethod", new Object[] { arg1, arg2 }, new…
Vivek Vermani
  • 1,934
  • 18
  • 45
10
votes
14 answers

Java: Is it ok to set Integer = null?

I have a function that returns an id number if the argument exists in the database. If not, it returns null. Is this begging for a null pointer exception? Negative id numbers are not permitted, but I thought it would be clearer to have non-existent…
Nick Heiner
  • 119,074
  • 188
  • 476
  • 699