Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 981
Nick Heiner Avatar asked Sep 13 '09 17:09

Nick Heiner


People also ask

What is a boxed variable?

Boxed values are data structures that are minimal wrappers around primitive types*. Boxed values are typically stored as pointers to objects on the heap. Thus, boxed values use more memory and take at minimum two memory lookups to access: once to get the pointer, and another to follow that pointer to the primitive.

What is a primitive type in Java?

A primitive type is predefined by the language and is named by a reserved keyword. Primitive values do not share state with other primitive values. The eight primitive data types supported by the Java programming language are: byte: The byte data type is an 8-bit signed two's complement integer.

Is int an object c#?

Everything in C# inherits from object , including int . Both reference and value types are derived from the ultimate base class Object.


2 Answers

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.

like image 152
Eric J. Avatar answered Sep 22 '22 12:09

Eric J.


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); 
like image 44
TM. Avatar answered Sep 20 '22 12:09

TM.