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
0 answers

Unboxing struct type C#

I am C# learner . I encountered with the delema . When I am unboxing (object variable into int variable and I encounter the error) . My code: namespace test1 { public struct Tst { public int x; public int y; } …
Jack
  • 97
  • 3
  • 10
0
votes
1 answer

Box object and intendedly lose unnecessary/extra data?

How can I box an object (e.g. from type B to type A) and intendedly lose the extra data? Example: class A { private string a; private DateTime time; public A(string a) { this.a = a; this.time = DateTime.Now; …
Tobias H
  • 71
  • 8
0
votes
0 answers

Java primitive-Object comparison: is the primitive autoboxed or the object unboxed?

According to the Java tutorial on autoboxing (and unboxing) Converting a primitive value (an int, for example) into an object of the corresponding wrapper class (Integer) is called autoboxing. The Java compiler applies autoboxing when a…
arcyqwerty
  • 10,325
  • 4
  • 47
  • 84
0
votes
0 answers

Comparing an int and Integer

In the case of below code, does the int gets boxed to an Integer or the Integer gets unboxed to an int? if (mAccount.getValue() != value) { // value is Integer object // do something // mAccount.getValue() returns int }
bighi
  • 247
  • 3
  • 13
0
votes
3 answers

Why Unboxing Object to long shows InvalidCastException?

I want to know why following shows an InvalidCastException: Object obj = 9; long num = (long)obj; //InvalidCastException After searching on net I find out Object considers 9 as Int so long doesn't exactly match Int. My question is why Object…
Vivek
  • 363
  • 8
  • 25
0
votes
1 answer

Why is Integer.parseInt(String str) returns int instead of Integer in Java?

I am asking this question because I have to a situation where I have the following two methods: public T get(Serializable id) and public T get(int id) I have to use the first method in most scenarios and the second method is already deprecated in…
WowBow
  • 7,137
  • 17
  • 65
  • 103
0
votes
2 answers

Assign Predefined Variable's Value To Object Type Variable In Boxing And UnBoxing Actions

I want to examine boxing and unboxing actions in C#. I defined variables in user defined class (it is my class). But when i want to use predefined varibles and then the strange error is occured. My code block like as below. public int i = 123; …
Mert Özoğul
  • 316
  • 3
  • 11
0
votes
7 answers

C# Type Conversion

I have two objects. Object A and Object B. Object A is an instance of a class that was generated from several XSD files. Used xsd.exe /c and compiled them. Now I have my new object. I also have a web service, returning something very similar to…
ist_lion
  • 3,149
  • 9
  • 43
  • 73
0
votes
1 answer

Converting a type to unboxed type

I am having trouble converting a type to unboxed type using derivingUnbox. I have tried below code but its giving error "parse error on input '->'" on line [t | Color -> Word32 |] type Color = (Word8,Word8,Word8) colorToWord32 :: Color ->…
Ahzaz
  • 88
  • 6
0
votes
1 answer

Manipulating Generics through auto/unboxing

public class Generics { public static T increaseBalance (T amount){ //say I want to increase the amount here, put it into finalBalance and return return finalBalance; } public static void main(String[] args){ …
Rushdi Shams
  • 2,423
  • 19
  • 31
0
votes
4 answers

Why doesn't Integer objects Unbox when compared with `==` operator?

Because sometimes it gets confusing. Lets say: Integer start=new Integer(10); Integer mid=new Integer(10); Integer end=new Integer(20); System.out.println(start
Kanwaljeet Singh
  • 1,215
  • 4
  • 15
  • 25
0
votes
3 answers

Unbox a number to double

Does some way to cast an unknown number to double exists? For example public static double Foo(object obj) { if (!obj.GetType().IsValueType) throw new ArgumentException("Argument should be a number", "obj"); …
Alex Zhukovskiy
  • 9,565
  • 11
  • 75
  • 151
0
votes
1 answer

Does checking against Boolean.TRUE/Boolean.FALSE avoid boxing/unboxing?

Let's say I have a Map and I want to filter out all the integers that have a boolean-value of true. Here's some code: for (Map.Entry e : map.entrySet()){ if (e.getValue() == true){ // Unboxing here //…
Lukas Knuth
  • 25,449
  • 15
  • 83
  • 111
0
votes
1 answer

Unbox value of type object to long

I had a variable of type object and I wanted to convert it to int in advance.? prev code val = Math.Max(((int)DatabaseHelper.ExecuteScalerCommand(query)), numbering.StartValue); Now I need to convert it to type long. I try to use…
fasadat
  • 1,025
  • 1
  • 12
  • 27
0
votes
2 answers

Want to know the internal code inferred by the compiler while unboxing

Code:- Integer value =null; int a = value; Output:- Exception in thread "main" java.lang.NullPointerException I understand that unboxing failed because there is not int value for null reference.But can anyone tell me the method invoked which lead…
Kumar Abhinav
  • 6,565
  • 2
  • 24
  • 35