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
26
votes
1 answer

Why does 'unbox.any' not provide a helpful exception text the way 'castclass' does?

To illustrate my question, consider these trivial examples (C#): object reference = new StringBuilder(); object box = 42; object unset = null; // CASE ONE: bad reference conversions (CIL instrcution 0x74 'castclass') try { string s =…
Jeppe Stig Nielsen
  • 60,409
  • 11
  • 110
  • 181
23
votes
1 answer

Boxing and unboxing when using out and ref parameters

Does boxing/unboxing occur when a method accepts an out/ref parameter of a ValueType?
brain_pusher
  • 1,507
  • 3
  • 21
  • 31
23
votes
4 answers

How does auto boxing/unboxing work in Java?

Since JDK 5.0, auto boxing/unboxing was introduced in Java. The trick is simple and helpful, but when I started testing different conversions between wrapper classes and primitive types, I get really confused how the concept of auto boxing works in…
Naruto Biju Mode
  • 2,011
  • 3
  • 15
  • 28
20
votes
7 answers

Why does unboxing require explicit casting in C#?

Boxing is the process of converting a value type into a managed heap object, which is implicit. Unboxing is the reverse process, for which the compiler requires an explicit cast. Since boxing stores the data type, why can't unboxing use that instead…
RJN
  • 696
  • 8
  • 17
19
votes
7 answers

Method overload resolution in java

Here is what I know about overload resolution in java: The process of compiler trying to resolve the method call from given overloaded method definitions is called overload resolution. If the compiler can not find the exact match it looks for…
ayush
  • 14,350
  • 11
  • 53
  • 100
17
votes
1 answer

How do I write a Data.Vector.Unboxed instance in Haskell?

I've got a numeric application that does a lot of work with negative logs of probabilities, which (since probabilities range from zero to one) take the values of positive doubles, or negative infinity (if the underlying probability was zero). I'm…
Noah Daniels
  • 355
  • 1
  • 11
16
votes
2 answers

how equal operator works with primitive and object type data

I know its a very basic question but I want to be clear about the concept. I want to know how == operator works in case of primitive and object type. For example Integer a = 1; int b = 1; System.out.println(a == b) how a is compared with b, whereas…
keepmoving
  • 1,813
  • 8
  • 34
  • 74
16
votes
3 answers

Why does the Java compiler sometimes allow the unboxing of null?

For example: int anInt = null; fails at compile time but public static void main(String[] args) { for (int i = 0; i < 10; i++) { System.out.println("" + getSomeVal()); } } public static int getSomeVal() { return new…
MilesHampson
  • 2,069
  • 24
  • 43
15
votes
4 answers

Does passing a value type in an "out" parameter cause the variable to be boxed?

I'm aware that boxing and unboxing are relatively expensive in terms of performance. What I'm wondering is: Does passing a value type to a method's out parameter cause boxing/unboxing of the variable (and thus a performance hit)? Can the compiler…
Justin Morgan - On strike
  • 30,035
  • 12
  • 80
  • 104
15
votes
2 answers

Boxed value unboxed then reboxed

FindBugs is giving me a warning about the following line, where invoiceNumber is an Integer object: text.append(String.format("%010d-", (invoiceNumber == null) ? 0 : invoiceNumber)); The warning is: "Boxed value is unboxed and then immediately…
DuncanKinnear
  • 4,563
  • 2
  • 34
  • 65
14
votes
1 answer

Android Studio: Unboxing of 'xxx' may produce 'java.lang.NullPointerException'

I'm following the Android book example: //Get the drink from the intent int drinkIdd = (Integer)getIntent().getExtras().get(EXTRA_DRINKID); Drink drink = Drink.drinks[drinkIdd]; And this project could be ran in Android Studio but with a yellow…
ChuckZHB
  • 157
  • 1
  • 1
  • 6
14
votes
3 answers

What is the difference between (Integer)y and new Integer(y) in java?

What is the difference between the following: Integer in = (Integer)y; and Integer in = new Integer(y); I want to convert int type to Integer type and vice versa. Here is my code for doing that: public class CompareToDemo { public static void…
sunny ranjan
  • 143
  • 6
13
votes
2 answers

Boxed Value Type comparisons

What i'm trying to achieve here is a straight value comparison of boxed primitive types. ((object)12).Equals((object)12); // Type match will result in a value comparison, ((object)12).Equals((object)12d); // but a type mismatch will not.…
johnDisplayClass
  • 269
  • 3
  • 11
12
votes
3 answers

Integer auto-unboxing and auto-boxing gives performance issues?

We are currently doing some iterations and other operations using x++; where x is an Integer and not an int. Operations may be repeated throughout some user operations on our system but nothing too complex or numerous like a Mathematical…
camiloqp
  • 1,140
  • 5
  • 18
  • 34
12
votes
3 answers

What does Box and Unbox mean?

Possible Duplicates: Why do we need boxing and unboxing in C#? What is boxing and unboxing and what are the trade offs? In C# what does "Box and Unbox" mean? Here's an extract from MSDN where I found the text. But this convenience comes at a…
GibboK
  • 71,848
  • 143
  • 435
  • 658
1
2
3
17 18