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
votes
4 answers

Return type is not returning as int when we perform unboxing

I am using autoboxing and unboxing in Java. Return type for Autoboxing is fine. no issues. but for unboxing the return type is coming as Integer instead of int. please find my coding on below and please let me know please package…
Ashok
  • 115
  • 3
  • 8
-1
votes
3 answers

How the comparisons are taking place in the code below

public class Application { public static void main(String[] args) { Integer a = new Integer(10); Integer b = new Integer(10); int x = new Integer(10); int y = new Integer(10); int p = 10; int q =…
Eddy
  • 19
  • 2
-1
votes
1 answer

What happens in this Boxing example in C#?

Jon Skeet, has an interesting post titled: "Why boxing doesn't keep me awake at night" where he benchmarks the performance of different ways of outputting an integer value. I am pretty sure the code below IS boxing, but why Jon is considering it NOT…
MaYaN
  • 6,683
  • 12
  • 57
  • 109
-2
votes
1 answer

Sonar Bug: Boxed value is unboxed and then again reboxed

I am unable to figure out where the boxed value is unboxed and again reboxed in below sample piece of code. Can some one pls help me in fixing this sonar bug. List floatList = new ArrayList<>(); Float hundred = 100F; Float zero = 0F; String…
Bhargav V
  • 9
  • 3
-2
votes
3 answers

Java DataStructures question boxing/unboxing

I am studying for a data structures exam tommorrow and need to know what lines in the following code are correct and which aren't and why Object obj = new Integer(42); Integer iObj = 43; iObj = obj;
Preston
  • 3
  • 3
-2
votes
2 answers

Boxing and unboxing is a myth?

In C# Int type is derived from ValueType. ValueType is derived from Object type. Thus Int is an Object. If every value type variable is already an Object, then what actually happens during boxing/unboxing?
TOP KEK
  • 2,593
  • 5
  • 36
  • 62
-3
votes
1 answer

Parsing or autoboxing or unboxing

So I already read what parsing is in the threads here, but wouldnt this be autoboxing or unboxing because it goes from an int to an Integer? This is the sentence When an integer is added to an array list declared as ArrayList, Java…
piper
  • 23
-3
votes
2 answers

Unboxing not able to understand

public class AccountManager { priavte Map accountTotals = new HashMap(); private int retirementFund; public int getbalance(String accountName) { **Integer Total = (Integer) accountTotals.get(accountName); if(total ==…
Sri
  • 177
  • 2
  • 2
  • 10
-4
votes
4 answers

Boxing/Unboxing in C#

I'm having trouble with a C# assignment dealing with boxing/unboxing. Here are the directions: Create an empty List of type object Add the following values to the list: 7, 28, -1, true, "chair" Loop through the list and print all values (Hint: Type…
-6
votes
1 answer

Passing Boolean to method that expects boolean

I am passing a Boolean value to a method that has a boolean parameter. When my value is null what happens? Will it assign a default value, like false to the argument in a method or will it cause any problems?
Droid
  • 45
  • 6
1 2 3
17
18