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
1
vote
1 answer

Does Convert.ToInt32(object) skip unboxing?

The IL produced from the following: object[] items = new object[] { 341, "qwerty" }; int item1FromConvert = Convert.ToInt32(items[0]); int item1FromCast = (int)items[0]; Is (according to LINQPad 4): IL_0001: ldc.i4.2 IL_0002: newarr …
markmnl
  • 11,116
  • 8
  • 73
  • 109
1
vote
4 answers

primitive == Wrapper converts to primitive == primitive or Wrapper == Wrapper?

I suppose that conversions described in jls are sorted according the priority. first has greate priority. jls Thus I solved that Boxing has greater priority than Unboxing. I decided to check this assumption. research following code: public class…
gstackoverflow
  • 36,709
  • 117
  • 359
  • 710
1
vote
5 answers

Whether It is Boxing Or Unboxing?

int i = 5; string str = i.ToString(); String str1=(String) i.ToString(); As Int 's are value Type and String 's are reference Type so Whether It is Boxing Or Unboxing ??? EDIT: Now For Second Statement Whether It is Boxing Or Unboxing ???
Dgan
  • 10,077
  • 1
  • 29
  • 51
1
vote
1 answer

Unboxing needed for tuples but not for types

Can someone explain the following, it seems a little inconsistent. This line of code is invalid: let l = [("Hi", 1); ("Ho", "One")] Because the tuples are different string*int vs string*string OK. This line of code is also invalid: let (m:…
Richard Dalton
  • 272
  • 2
  • 6
1
vote
3 answers

Programming the "dynamic" type in C#

When we use dynamic type over object type weather we can overcome Boxing/UnBoxing overhead ? void Print(dynamic p) { Console.WriteLine(string.Format("{0} : {1}", p.GetType(),p)); } void Print(object p) { …
dinesh
  • 635
  • 1
  • 10
  • 23
1
vote
3 answers

Can't access members of a class after unboxing

I am learning advanced C#.In the following Code i am trying to do Event handling i get Error while accessing members of class sender after unboxing //Compiler is not letting me use these names // Console.WriteLine("Sender is {0} and…
Charlie
  • 4,827
  • 2
  • 31
  • 55
1
vote
2 answers

Java: Unboxing values stored in an Object to an unknown type

I am building something like a data flow graph, with nodes and connections that pass data between them. The base class in this case is ValueTarget, which has a previous and a next target to pass data back and forth. Other classes extend this…
Jay
  • 237
  • 2
  • 14
1
vote
3 answers

C# type-casting sender

So I came across this little gem in our codebase the other day, and I wanted to try and see if the person who wrote it was just lazy, or knew something that I don't. A standard event handler was written like this (I will - private void…
thebringking
  • 1,339
  • 1
  • 15
  • 31
1
vote
2 answers

How to prevent unboxing - boxing memory overhead when reading arbitrary sql rows

I'm writing a class to represent a row from a SQL query. I want the field data to be accessed via the indexer property of the class. This is straightforward enough if I load the data into an internal List of Object. I've already tried this and am…
Steve
  • 2,153
  • 4
  • 22
  • 31
1
vote
3 answers

Is unboxing expensive?

Is unboxking expensive that it's better avoiding it? From this java tutorial: public class ValueOfDemo { public static void main(String[] args) { // this program requires two // arguments on the command line if…
Rollerball
  • 12,618
  • 23
  • 92
  • 161
1
vote
1 answer

How can I guarantee that my class can be unboxed as a a particular type?

I created an IValueConverter that converts a bool to a System.Windows.Visibility object (it does the opposite of BooleanToVisibilityConverter). It works fine, except when I try to use it on an Observable object. Observable is a class I…
hypehuman
  • 1,290
  • 2
  • 18
  • 37
1
vote
1 answer

Unboxing modifies items collection

Having a problem with this Winforms project. Trying to use the SelectedIndexChanged event on a combBox that that was populated with the dictionary propList via: comboBox1.DataSource = new BindingSource(propList, null); comboBox1.DisplayMember =…
Ruud A.
  • 123
  • 2
  • 10
1
vote
2 answers

Which of the following is true about the second statement? (unboxing & autoboxing)

I have looked all over the internet to try and solve this problem. Can anyone answer this correctly and explain why? Thank you so much! Look at the following code. Integer myNumber; myNumber = 5; Which of the following is true about the second…
Travis
  • 636
  • 8
  • 11
0
votes
1 answer

How to box VAR1 and unbox VAR2 according to the type of VAR1

Imagine having an integer var aaa = (int)1; -------> here I have an int var bbb = (object)aaa;-----> bbb is an object but the type is kept... var ccc = aaa.GetType();---> ... in fact here I still have an integer now I define a new variable var ddd…
Patrick
  • 3,073
  • 2
  • 22
  • 60
0
votes
1 answer

Fix Unboxing Warning with a Hashtable Object Possibly being NULL

I am working on some projects to better acclimate myself to C#. I have this code example below: Hashtable hashtable = new Hashtable(); for(int i = 0; i < s.Length; i++) { if(hashtable.ContainsKey(s[i])) { hashtable[s[i]] =…
Sacred
  • 147
  • 1
  • 12