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

After converting a operator back to a char from a double, how do I make use of it?

I am writing a program that evaluates a LISP expression through iteration. LISP expressions are as follows: Adding two and two would be written in LISP as: (+ 2 2). The LISP expression (* 5 4 3 2 1) would be evaluated to five factorial. To do this,…
Atache
  • 169
  • 1
  • 13
0
votes
1 answer

Why is it not possible to unbox erased types in Java?

The two add methods in this class have the same erased signature: class extend { Integer add (Integer a, Integer b) { return a + b; } Type add (Type a, Type b) { return a + b; } } This makes it impossible…
ceving
  • 21,900
  • 13
  • 104
  • 178
0
votes
8 answers

Does .Net typecast when an object is used in collection using generics?

Does .net CLR typecast the objects to the ones mentioned in the collection declaration? If i declare a List lststrs= new List(); lststrs.add("ssdfsf"); does .net typecasts this object while adding and retriving????? Well i think…
Ravisha
  • 3,261
  • 9
  • 39
  • 66
0
votes
2 answers

How to make an unboxed array of floats I can get a Ptr to

I am trying to do some work with HopenGL and I need a Ptr that points to a array of floats. From what I have read uarray and storableArray seem to be the way to go, in some combination some way.
Jonathan Fischoff
  • 1,467
  • 12
  • 22
0
votes
2 answers

Un Boxing a class object - No Error?

Probably I may be confused with boxing and unboxing. Consider the following statement from MSDN: "Unboxing is an explicit conversion from the type object to a value type or from an interface type to a value type that implements the …
0
votes
1 answer

Does the boxed value type have the same address as the actual value type in the object?

From MSDN unbox does An object reference is pushed onto the stack. The object reference is popped from the stack and unboxed to a value type pointer. The value type pointer is pushed onto the stack. Isn't the object reference identical to a value…
colinfang
  • 20,909
  • 19
  • 90
  • 173
0
votes
1 answer

How to check type of unboxing nullable type with value = null?

Look at this method: Dictionary ViewModelParams = new Dictionary(); AddParam(string paramKey,value) { viewModelParams.Add(paramKey,value); } T GetParam(string paramKey) { if(viewModelParams[paramKey] is T) …
Reza ArabQaeni
  • 4,848
  • 27
  • 46
0
votes
2 answers

Automatic unboxing

I have an array of objects (object[]). All the items in this array have the same type (unknown at compile time). I need to convert this array in a typed array, that is, if the items are integers, I want to get an int[]. I've looked into the…
gregseth
  • 12,952
  • 15
  • 63
  • 96
0
votes
3 answers

confusion about doublevalue() in hash tables

// Demonstrate a Hashtable import java.util.*; class HTDemo { public static void main(String args[]) { Hashtable balance = new Hashtable(); Enumeration names; String str; double bal; balance.put("John…
-1
votes
1 answer

Is it boxing or unboxing?

I often get confused with boxing and unboxing. I mean I understand that both of them mean casting from System.Object to a value type which inherits from it (e.g. System.Int32 or System.Double) or contrariwise. And casting from System.Object to a…
user20311258
-1
votes
1 answer

Compiler does not unbox lambda parameter int

So i'm trying to pass a Function to perform an operation on a int[x] value. So this is my method: public void apply(int index, UnaryOperator func) { V[index] = func.apply(V[index]); } This works: register.apply(0, new…
Toerktumlare
  • 12,548
  • 3
  • 35
  • 54
-1
votes
2 answers

auto boxing and un boxing what is the difference and when to use it?

hi all i am still beginner to java may someone explain to me what is the difference between auto-boxing and Un-boxing use for and when to use that? //this is my sample code ArrayList listOfDoubles = new ArrayList(); …
Steven Y.
  • 41
  • 1
  • 6
-1
votes
4 answers

How to cast to a type passed to a function?

I have this code which casts controls to a TextBox: foreach (Control c in Controls) if (c.GetType() == typeof(TextBox)) (c as TextBox).Clear(); And I would like to encapsulate it in a function, where I pass in the type at runtime. …
이재국
  • 5
  • 3
-1
votes
2 answers

What's the point of autoboxing primitives in Java?

What's the point of having Integer, Boolean etc and call them "...boxing" if they don't behave like boxed objects that you can pass "by ref" and do unboxing to change their value? Here's an example of "unboxing" that I actually found out isn't…
shinzou
  • 5,850
  • 10
  • 60
  • 124
-1
votes
2 answers

Generics with autoboxing and unboxing of primitives

Why autoboxing and unboxing of primitives not happens with Generics Java. public static T addNumber(T a , T b) { int c = a*b; System.out.println(c); return c; } Here why * operation can't be performed and why can't return…
Pooja
  • 31
  • 5
1 2 3
17
18