Questions tagged [unboxing]

Unboxing is the opposite of boxing. Unboxing is the conversion of a wrapper object to their corresponding primitive value the wrapper stores.

Several object-oriented languages, such as Java, differentiate between primitive types, such as int, with an object reference type, such as String. Java provides eight primitive values, commonly the int, long, double, boolean, char, and sometimes the byte, short and float. Primitives are the simplest data types, their type names are usually keywords, and several operations, such as inequality and arithmetic operators work only on primitive data types.

However, in some cases such as polymorphism and converting an Object to a Primitive and back, sometimes you may need to use a wrapper class, a special class whose object contains only one field, its corresponding primitive values (as well as certain methods). The names of the primitive type's corresponding wrapper class can be distinguished by the name of the primitive by beginning with an uppercase letter, and being written out in full.

These wrapper classes have a special capability called unboxing, where the compiler automatically converts the wrapper Object to its corresponding primitive, and allows performing operations which are otherwise restricted to the primitives, such as using inequality symbols <, <=, >, >=, or passing a primitive value where it otherwise requires an Object.

Java Examples:

Using relational inequality symbols <, <=, >=, > (operator == and != are always for reference equality or inequality for any Object respectively, no unboxing).

Integer a = 15;
Integer b = 0;
Double x = 7.5;
Double y = 15.000000000000000000000000001; // Decimal part discarded
Character ch = 'A';

/* Comparing these wrappers using inequality symbols involve unboxing. */
/* The compiler converts these Objects to its corresponding primitives, */
/* before testing inequality. */
System.out.println(x + " < " + a + " is " + (x < a));
System.out.println(x + " < " + b + " is " + (x < b));
System.out.println(a + " > " + b + " is " + (a > b));

System.out.println(ch + " <= " + 65 + " is " + (ch <= 64)); // 'A' has ASCII code 65
System.out.println(ch + " <= " + 65 + " is " + (ch <= 65));
System.out.println(a + " >= " + b + " is " + (a >= b));

Adding an integer value as an int literal to an ArrayList of Integers.

ArrayList<Integer> vector = new ArrayList<Integer>();
vector.add(1); // int literal '1' is boxed to an Integer

// The returned value, otherwise an Integer, unboxed to int with value 1
int n = vector.get(0);
265 questions
345
votes
10 answers

Performance surprise with "as" and nullable types

I'm just revising chapter 4 of C# in Depth which deals with nullable types, and I'm adding a section about using the "as" operator, which allows you to write: object o = ...; int? x = o as int?; if (x.HasValue) { ... // Use x.Value in here } I…
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
152
votes
9 answers

What is boxing and unboxing and what are the trade offs?

I'm looking for a clear, concise and accurate answer. Ideally as the actual answer, although links to good explanations welcome.
Keith
  • 150,284
  • 78
  • 298
  • 434
107
votes
2 answers

Differences in auto-unboxing between Java 6 vs Java 7

I have noted a difference in auto unboxing behavior between Java SE 6 and Java SE 7. I'm wondering why that is, because I can't find any documentation of changes in this behavior between these two versions. Here's a simple example: Object[] objs =…
Morty
  • 1,706
  • 1
  • 12
  • 25
87
votes
6 answers

What is the difference between boxing/unboxing and type casting?

What is the difference between boxing/unboxing and type casting? Often, the terms seem to be used interchangeably.
Faith
74
votes
6 answers

Boxing and unboxing with generics

The .NET 1.0 way of creating collection of integers (for example) was: ArrayList list = new ArrayList(); list.Add(i); /* boxing */ int j = (int)list[0]; /* unboxing */ The penalty of using this is the lack of type safety and performance…
Itay Karo
  • 17,924
  • 4
  • 40
  • 58
64
votes
4 answers

Why can't I unbox an int as a decimal?

I have an IDataRecord reader that I'm retrieving a decimal from as follows: decimal d = (decimal)reader[0]; For some reason this throws an invalid cast exception saying that the "Specified cast is not valid." When I do reader[0].GetType() it tells…
mezoid
  • 28,090
  • 37
  • 107
  • 148
59
votes
2 answers

Difference in behaviour of the ternary operator on JDK8 and JDK10

Consider the following code public class JDK10Test { public static void main(String[] args) { Double d = false ? 1.0 : new HashMap().get("1"); System.out.println(d); } } When running on JDK8, this code prints…
SerCe
  • 5,826
  • 2
  • 32
  • 53
45
votes
5 answers

Comparing boxed value types

Today I stumbled upon an interesting bug I wrote. I have a set of properties which can be set through a general setter. These properties can be value types or reference types. public void SetValue( TEnum property, object value ) { if (…
Steven Jeuris
  • 18,274
  • 9
  • 70
  • 161
45
votes
1 answer

Automatic differentiation with unboxed vectors

Is there a Haskell library for automatic differentiation which works with unboxed vectors? The grad function from Numeric.AD requires an instance of Traversable, which Data.Vector.Unboxed is not.
Grzegorz Chrupała
  • 3,053
  • 17
  • 24
43
votes
2 answers

Findbugs issue with "Boxing/unboxing to parse a primitive" with Integer.valueOf(String)

I have this piece of code: public void someMethod(String id) { someOtherMethod(Integer.valueOf(id)); } public void someOtherMethod(int id) { // do something with id } And on that second line, Findbugs is throwing this…
mac
  • 2,672
  • 4
  • 31
  • 43
42
votes
8 answers

How is it that an enum derives from System.Enum and is an integer at the same time?

Edit: Comments at bottom. Also, this. Here's what's kind of confusing me. My understanding is that if I have an enum like this... enum Animal { Dog, Cat } ...what I've essentially done is defined a value type called Animal with two defined…
Dan Tao
  • 125,917
  • 54
  • 300
  • 447
37
votes
6 answers

Why do some languages need Boxing and Unboxing?

This is not a question of what is boxing and unboxing, it is rather why do languages like Java and C# need that ? I am greatly familiar wtih C++, STL and Boost. In C++ I could write something like this very easily, std::vector dummy; I have…
Khaled Alshaya
  • 94,250
  • 39
  • 176
  • 234
33
votes
6 answers

java.lang.Integer cannot be cast to java.lang.Long

I'm supposed to receive long integer in my web service. long ipInt = (long) obj.get("ipInt"); When I test my program and put ipInt value = 2886872928, it give me success. However, when I test my program and put ipInt value = 167844168, it give me…
inayzi
  • 459
  • 2
  • 6
  • 13
30
votes
8 answers

How to unbox from object to type it contains, not knowing that type at compile time?

At the run-time I get boxed instance of some type. How to unbox it to underlying type? Object obj; String variable = "Some text"; obj = variable // boxing; // explicit unboxing, because we know the type of variable at compile time. var x =…
Paul Kyrejto
  • 1,285
  • 3
  • 12
  • 19
28
votes
6 answers

Boxing vs Unboxing

Another recent C# interview question I had was if I knew what Boxing and Unboxing is. I explained that value types are on Stack and reference types on Heap. When a value is cast to a reference type, we call it boxing and vice versa. Then he asked me…
Houman
  • 64,245
  • 87
  • 278
  • 460
1
2 3
17 18