What does it mean to say a type is "boxed"?
Asked Answered
C

6

77

I have heard of types being referred to as "boxed" in some languages.

In Java, I have heard of "autoboxing". What is this? Is it having wrapper classes for a type? How would my code change if I'm working with boxed or unboxed types?

Cultus answered 13/9, 2009 at 17:23 Comment(0)
F
96

Some data types are considered "primitive", meaning they are not treated like an object and don't have the properties of an object.

On most platforms, integers and characters are examples of types that are primitive but can be boxed.

Boxing means wrapping them in an object so they have the behavior of an object.

The exact meaning and behavior depends on the language you're using. Some languages (such as Smalltalk... at least waaay back when I was doing it...) don't allow any primitive types and consider everything to be an object, but there's a performance penalty associated with that because, at the end of the day, the processor needs to work with raw numbers and raw memory to do useful work. If you want to add two integers that have been boxed, for example, behind the scenes they are "unboxed" into primitive types, the numbers are added, and they are then boxed back into a new integer.

Figurative answered 13/9, 2009 at 17:26 Comment(0)
A
38

More specific information for Java:

Autoboxing allows java to automatically convert things like boolean and int to their Object versions Boolean and Integer in most cases. It also allows the reverse to happen.

For example:

int a = 3; // no boxing is happening
Integer b = 3;  // newer versions of java automatically convert the int 3 to Integer 3
int c = b;  // these same versions also automatically convert Integer 3 to int 3

Older versions of java that do not have autoboxing will require this code to do the same thing:

int a = 3;  // works the same
Integer b = new Integer(3);  //must set up a Integer object manually
int c = b.intValue(); //must change Integer object to a primitive

However, there are some scenarios where you still have to do things manually. For example, imagine you have a class with two methods like so:

assertEquals(int a, int b);
assertEquals(Object a, Object b)

Now, if you try to do this:

Integer a = 3;
int b = 3;
assertEquals(a, b);  // this will not compile

The reason this doesn't work is because it cannot figure out whether it should unbox a to an int or box b to an Integer. Therefore it is ambiguous which method signature should be called. To fix this you can do one of these:

assertEquals((int) a, b);
assertEquals(a, (Integer) b);
Aceous answered 13/9, 2009 at 17:37 Comment(1)
great walkthrough and examples!Marcellusmarcelo
D
13

Yes, boxing means taking a value type and wrapping it in a reference type. Since java introduced autoboxing you can do:

void foo(Object bar) {}
//...
    foo(1);

And java will automatically turn the int 1 into an Integer. In previous versions you'd have to do:

foo(new Integer(1));

Autoboxing is most useful in java when working with generics, since you can't use primitives with generics, so to store ints in a list, you'd have to make a List<Integer> and put the ints into the list boxed.

Dunlap answered 13/9, 2009 at 17:30 Comment(0)
M
5

A boxed type means that the values are allocated in a block on the heap and referenced through a pointer. This is good for uniformity in the implementation of the runtime (it makes it easier to have generic functions, etc), at the cost of an additional indirection.

Microchemistry answered 13/9, 2009 at 17:28 Comment(0)
P
2

Boxed means that they took a regular value type and created an object around it. Sort of like putting it in a box. This is generally to be avoided, because of the overhead of constructing the object.

Phytography answered 13/9, 2009 at 18:52 Comment(0)
S
1

Generally when you work with collections, you're dealing with arrays of Objects. In languages like Java, there is a difference between a primitive and an Object. When a primitive is "boxed", it's essentially just a wrapper around a primitive so it plays nice with the rest of the framework that's expecting an Object.

Autoboxing is just the act of putting a primitive into an object or pulling a primitive out of an object transparently so you don't have to worry about the extra step of doing it yourself.

Stob answered 13/9, 2009 at 17:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.