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

Quick ValueTuple initialization with 1 argument

I want to create a simple way to create a matrix-like structure for users. This means that the user can define rows and columns. For now its looks like this: var matrix = new() { new() { Item1, Item2 } // Row new() { Item3, Item4 } //…
0
votes
0 answers

How to check if a parameter exists or not?

So I have a video inventory program, Where the user creates a object of the class video by initializing the constructor Video video=new Video(String name){ this.name=name } Now I have three more parameterized constructor Video video=new…
user17285108
0
votes
0 answers

Why explicit conversion from object to long (Unboxing) is not allowed in C#?

I was reading unboxing and came across this code: object obj = 22; long l = (long)obj; when I ran this code, it throws an exception InvalidCastException I do not understand why? For example Microsoft documentation on Boxing and Unboxing shows…
0
votes
1 answer

Mocked interface is null

I am trying to mock a DAO with JMockit: public interface MyDao { Details getDetailsById(int id); } With this test class: public class TestClass { @Test public void testStuff(final MyDao dao) throws Exception { new…
Brian
  • 2,375
  • 4
  • 24
  • 35
0
votes
0 answers

Unboxing and Boxing in Java, but something feels strange

Integer a1=164; Integer a2=164; System.out.println(a1==a2); System.out.println(a1.equals(a2)); Integer a3=new Integer(164); Integer a4=new Integer(164); …
0
votes
1 answer

Integer arithmetic in equals expression

I know the first expression evaluates to false because these are two distinct Integer objects. I'm not sure why the second expression evaluates to true. public static void main(String[] args) { System.out.println(new Integer(1000)==new…
Gonen I
  • 5,576
  • 1
  • 29
  • 60
0
votes
2 answers

Unexpected behaviour with Java unboxing

Map map = new HashMap<>(); map.put(1, 1); int value = map.get(2); System.out.println(v); On executing the above code I find below exception Exception in thread "main" java.lang.NullPointerException but if place an Integer in the…
0
votes
1 answer

getting an error in unboxing in c# stating name does not exist in current context?

class Program { static void Main(string[] args) { Console.WriteLine("enter id"); int id = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("enter name"); string name =…
shubham
  • 3
  • 1
0
votes
1 answer

Scala is boxing and unboxing non parametric function arguments and result. Why? Posible duplicate

According to the debugger, in the following snippet scala is boxing and unboxing the Char argument and later boxing and unboxing the Boolean result. Why? How to avoid that? object Test{ def user(predicate: Char => Boolean): Boolean = { …
Readren
  • 994
  • 1
  • 10
  • 18
0
votes
1 answer

Why list of objects throws an exception when trying to update it's value?

Why the following code throws ArrayStoreException on line 3: int[] ar = {2, 4}; List list = Arrays.asList(ar); list.set(0, 3); Unboxing should be performed here from Integer to int but it doesn't.
Artem S
  • 11
  • 3
0
votes
3 answers

Converting from Integer wrapper class to int primitive class

I've been trying to convert an Integer Wrapper class to int primitive class. I haven't yet found a proper way to make the code compile. I'm using Intellij IDEA, Java 11 Amazon Coretto, but I need to run it on a computer that runs java 8. Here's the…
AngryWeeb
  • 65
  • 1
  • 9
0
votes
2 answers

Seeking documentation on compiler-generated lambdas to convert specialized to generic functional objects in Java 8

I’ve been working with Java 8 lambdas for several months and just now discovered a behavior that I don’t remember seeing any mention of in the Java docs or programming websites. It can be seen in the following code: public class…
0
votes
1 answer

Evaluating Boxed Type as Type in C#

My application uses a boxed Type object for which I later need to evaluate that it is of type Type, before unboxing. This is implemented like so... public void MyFunc(params Object[] args) { …
Jim Fell
  • 13,750
  • 36
  • 127
  • 202
0
votes
0 answers

Is there a better way to cast a UDT to another type?

I have a class that contains a property "value" that contain any data type and therefor is an object. In the included example, the "value" is the type "nonNegativeInteger". I need to be able to cast the structure to double and the only way I have…
Kingman B
  • 11
  • 2
0
votes
1 answer

How to compare two boxed variables (not knowing if they each can be casted to the other's type, or whether they are value or reference types)?

I have the watches with the values written below and a property with the accessors written below. Although the values are boxed strings and their types are the same, and the actual text in the strings is exactly the same, the condition d.Value.Value…
silviubogan
  • 3,343
  • 3
  • 31
  • 57