Questions tagged [boxing]

Boxing is when a value type is wrapped in a reference-type wrapper for the purposes of using it when polymorphism (conversion to Object or an interface) is required.

In Java and some similar languages there is a differentiation between primitives (or value types) and reference types. The primitive types work as simple values and, when used as a parameter to methods, do not change the original operand. Reference types on the other hand can be modified when passed into methods and have associated classes that define objects of that type (whereas primitives are more "built-in"). When viewed from the perspective of language such as C or C++, instances of reference types can be thought of as pointers that are automatically de-referenced.

Boxing occurs when a primitive value type is used in place of a corresponding reference type. In Java, for example, if a value of type int is used where an Integer is expected, the compiler will automatically wrap the value in an Integer object.

588 questions
-1
votes
2 answers

Method to box primitive type

Here is a table of primitive types and their equivalent wrapper class. Primitive type Wrapper class ============== ============= boolean Boolean byte Byte char Character float Float int Integer …
MaxZoom
  • 7,619
  • 5
  • 28
  • 44
-1
votes
1 answer

Attributing the selected item to a certain type

I've got a combo box with items of type Something. Upon an event, I want to assign that item to a variable. At the moment, since SelectedItem is of type Object, I need to unbox it to the desired type like this. Something something =…
Konrad Viltersten
  • 36,151
  • 76
  • 250
  • 438
-1
votes
1 answer

When do I use Boxing and Unboxing?

I know boxing convert value type to object and unboxing convert object to a value type. Example: int num = 1; Object obj = num; //boxing int num2 = (int)obj //unboxing But when I use it in the real life (when I write some code)? I never…
Avi Cohen
  • 55
  • 5
-1
votes
1 answer

Retrieving a generic type value from an object dictionary without exceptions

Is there a generic way (preferably with an extension method) that you can retrieve values (could be decimal, short, int or string) casted to the right type from a Dictionary without it failing when the key does not exist?
John Smith
  • 36
  • 4
-1
votes
2 answers

Boxing the string in c#

Whenever I Search for Boxing in c#, I come across a cliche example like following: string s = "something"; Object o = s; This is very simple to understand that a value type is cast into a very generic reference type at run time. Well and good. I…
Lost
  • 12,007
  • 32
  • 121
  • 193
-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
1 answer

NaN payload storing 64 bit ints

I recently found about the topic nan boxing, I'm also trying to build a dynamic typed programming language so nan boxing seemed the choice to represent the types but I'm still a lot confused about it, one of them is how would i store data like a 64…
PoLLeN
  • 61
  • 7
-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
1 answer

What allocates more, boxing paramaters or params array?

Which of the following calls allocates more garbage? void LogParams(string format, params object[] args) { } void LogArgs(string format, object arg0, object arg1, object arg2) { } LogParams("Hello, {0}, {1}, {2}", "Tom", "Dick",…
JimmyDeemo
  • 313
  • 1
  • 15
-2
votes
1 answer

Parsing Long value to Long?

I'm working with a quite old codebase, so much of deprecated or outdated code exists. Most of it is simple pre-boxing. But this one raised a question with me: private java.lang.Long version = Long.valueOf(-1L); Does the -1L not define the value as…
Mathijs Segers
  • 6,168
  • 9
  • 51
  • 75
-2
votes
4 answers

Does boxing cause performance issues? How can I prevent boxing?

Does boxing cause performance issues in my code? How can I prevent boxing? void Main() { AreEqual(12, 13); } public static bool AreEqual(T a, T b) { return a.Equals(b); } IL: IL_0000: nop IL_0001: ldc.i4.s 0C…
Mohamad Shiralizadeh
  • 8,329
  • 6
  • 58
  • 93
-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
-4
votes
2 answers

Boxing and Unboxing concept

If I write int i=100; then does it get stored as int in memory taking 4 Bytes or stored as object, and at the time of it's retrieval unboxing concept used (int extracted from object class type object).
-4
votes
1 answer

Collect primitive array values into a collection of type Map

How can I convert an array of primitive int, long, or double values into a collection of type Map? import java.util.*; import java.util.function.*; import java.util.stream.*; public class PrimitiveStreamCollection { private static final…
Mr. Polywhirl
  • 42,981
  • 12
  • 84
  • 132
1 2 3
39
40