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

How to avoid boxing/unboxing of a large dynamic object?

I need to multiply two relatively large matrices and do it many times (in a loop). However, the format (type of objects) how these matrices are stored in the memory should be chosen by user. There are three possibilities: double [,] M1; double [,]…
user3786219
  • 177
  • 1
  • 2
  • 11
0
votes
1 answer

Java Integer Constant - Unboxing

Given this code change: int count = 0; Replaced by: int count = NumberUtils.INTEGER_ZERO; I relied on Apache NumberUtils to change, just for the sake of constants order. What I wanted to know is if there is any drawback for performing this change.…
Sebastian Motavita
  • 355
  • 1
  • 3
  • 9
0
votes
3 answers

Java boxing or unboxing

I found an example where I cannot find the number of boxing and unboxing in the Java code below : Integer x = 5; int y = x + x; I would say that there is one type of unboxing (int y = x + x), but I am not sure about that. Is there any boxing as…
ihavename
  • 25
  • 7
0
votes
0 answers

A generic converter in WPF

I have a converter in my WPF application, as follows:
Cod Fish
  • 917
  • 1
  • 8
  • 37
0
votes
2 answers

Java boxing and unboxing

I have an example in which I cannot determine the number of boxing(s) and unboxing(s), which taking place in the Java code below : int x = 5; Integer y = x + x; From my point of view I see one type of boxing (Integer y = x + x). Am I wrong? Is…
ihavename
  • 25
  • 7
0
votes
1 answer

In this example, is boxing used when unboxing is needed?

I have some difficulty understanding the following part from Programming Language Pragmatics, by Scott C# and more recent versions of Java perform automatic boxing and unboxing operations that avoid the wrapper syntax in many cases: ht.put(13,…
Tim
  • 1
  • 141
  • 372
  • 590
0
votes
0 answers

How does unboxing and auto boxing occur when a method reference is passed for a generic method?

Here is a piece of code: @FunctionalInterface interface NumericFunc{ int fact(T[] a,T b); } class MyStringOps{ static int counter(T[] a,T b){ int count=0; for(int i=0;i
0
votes
1 answer

Why 'if let' does not seem to unbox a value as before in Swift 3 in Xcode 8.3 beta?

Unlike before, I was surprised to see that 'title' is now an optional (the compiler now generates the waning : String interpolation produces a debug description for an optional value; did you mean to make this explicit?). How it comes the 'if let…
Stéphane de Luca
  • 12,745
  • 9
  • 57
  • 95
0
votes
1 answer

Java Autoboxing/Unboxing and Generic Type issue

I'm testing my knowledge on Generics in Java, and I'm coming across an issue. The code return (t%2==0); in my IsEven.java wont compile, as well as int comparison = t.compareTo(memberT); in my GreaterThan.java I assume this is an error with how I…
0
votes
1 answer

Unboxing. Copy of fields to stack in most of cases

Richter "CLR via C#" famous book I understand, when do unboxing, it returns pointer to unboxed value on heap, but i cant get pointer in C#, so there is copy of fields from heap to stack done (answered here). But, when i do Console.WriteLine(v + ",…
zzfima
  • 1,528
  • 1
  • 14
  • 21
0
votes
2 answers

Boxing and UnBoxing on Android

I'm developing an Android application. I have the following interface: public interface IDBAdapter { public enum Table { ... } public int insertItem(Table table, Object item); public boolean removeItem(Table table, long…
VansFannel
  • 45,055
  • 107
  • 359
  • 626
0
votes
2 answers

Assigning an array of structure to another array of same structure

In Vb.net I am trying to assign an array of structure to another array of same structure Dim info() As assemblyInfo Dim info2() As assemblyInfo Structure assemblyInfo Dim Name As String Dim ClassTpys() As ClassTyp End…
Anshul
  • 1
0
votes
1 answer

Boxing and unboxing generics with casting to interface

I Have code like bellow public interface IFoo { int One { get; set; } string Two { get; set; } } public class Foo : IFoo { public int One { get; set; } public string Two { get; set; } } public class SomeDto where T :…
user3333010
  • 113
  • 2
  • 9
0
votes
0 answers

Simple way to unbox an array of Doubles in Java

I'm wondering if there is a simple one liner that would unbox an array of Doubles into an array of doubles. Obviously it could be done simply using a for loop, but I'm just curious to see if Java has some fancy auto-unboxing feature that allows the…
NateW
  • 908
  • 1
  • 8
  • 28
0
votes
1 answer

Which one of these approaches is better or are they the same?

I want to know which of these two FiniteStateMachine.AddState() methods is better than the other, for example, does one require unboxing/boxing? or do both require unboxing/boxing? and if any of them is better than the other, which one? public…
Lunatrap
  • 15
  • 5