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
45
votes
5 answers

Comparing boxed value types

Today I stumbled upon an interesting bug I wrote. I have a set of properties which can be set through a general setter. These properties can be value types or reference types. public void SetValue( TEnum property, object value ) { if (…
Steven Jeuris
  • 18,274
  • 9
  • 70
  • 161
43
votes
2 answers

Findbugs issue with "Boxing/unboxing to parse a primitive" with Integer.valueOf(String)

I have this piece of code: public void someMethod(String id) { someOtherMethod(Integer.valueOf(id)); } public void someOtherMethod(int id) { // do something with id } And on that second line, Findbugs is throwing this…
mac
  • 2,672
  • 4
  • 31
  • 43
41
votes
3 answers

Java signed zero and boxing

Lately I've written a project in Java and noticed a very strange feature with double/Double implementation. The double type in Java has two 0's, i.e. 0.0 and -0.0 (signed zero's). The strange thing is that: 0.0 == -0.0 evaluates to true, but: new…
uhz
  • 2,468
  • 1
  • 20
  • 20
39
votes
3 answers

How is the boxing/unboxing behavior of Nullable possible?

Something just occurred to me earlier today that has got me scratching my head. Any variable of type Nullable can be assigned to null. For instance: int? i = null; At first I couldn't see how this would be possible without somehow defining an…
Dan Tao
  • 125,917
  • 54
  • 300
  • 447
38
votes
2 answers

Why the compiler emits box instructions to compare instances of a reference type?

Here is a simple generic type with a unique generic parameter constrained to reference types: class A where T : class { public bool F(T r1, T r2) { return r1 == r2; } } The generated IL by csc.exe is : ldarg.1 box …
Pragmateek
  • 13,174
  • 9
  • 74
  • 108
38
votes
6 answers

Does var keyword in C# cause boxing?

My boss forbids me to use var as it would cause boxing and slowing down the app. Is that true?
msfanboy
  • 5,273
  • 13
  • 69
  • 120
37
votes
6 answers

Why do some languages need Boxing and Unboxing?

This is not a question of what is boxing and unboxing, it is rather why do languages like Java and C# need that ? I am greatly familiar wtih C++, STL and Boost. In C++ I could write something like this very easily, std::vector dummy; I have…
Khaled Alshaya
  • 94,250
  • 39
  • 176
  • 234
32
votes
4 answers

Is there a built-in Java method to box an array?

Is there a standard method I can use in place of this custom method? public static Byte[] box(byte[] byteArray) { Byte[] box = new Byte[byteArray.length]; for (int i = 0; i < box.length; i++) { box[i] = byteArray[i]; } return…
The Student
  • 27,520
  • 68
  • 161
  • 264
30
votes
5 answers

How to form CGPoint array in Objective-C?

I want to get this structure: CGPoint addLines1[] = { CGPointMake(30.0, 150.0), CGPointMake(41.67, 145.19), CGPointMake(53.33, 103.25), CGPointMake(65.0, 131.67), CGPointMake(76.67, 106.11), CGPointMake(88.33, 110.20), …
Mladen
  • 25,578
  • 11
  • 39
  • 48
30
votes
10 answers

How to test whether a value is boxed in C# / .NET?

I'm looking for a way to write code that tests whether a value is boxed. My preliminary investigations indicate that .NET goes out of its way to conceal the fact, meaning that GetType() and IsValueType don't reveal the difference between a boxed…
Reb.Cabin
  • 5,426
  • 3
  • 35
  • 64
29
votes
2 answers

Compare two integer objects for equality regardless of type

I'm wondering how you could compare two boxed integers (either can be signed or unsigned) to each other for equality. For instance, take a look at this scenario: // case #1 object int1 = (int)50505; object int2 = (int)50505; bool success12 =…
caesay
  • 16,932
  • 15
  • 95
  • 160
28
votes
6 answers

Boxing vs Unboxing

Another recent C# interview question I had was if I knew what Boxing and Unboxing is. I explained that value types are on Stack and reference types on Heap. When a value is cast to a reference type, we call it boxing and vice versa. Then he asked me…
Houman
  • 64,245
  • 87
  • 278
  • 460
28
votes
4 answers

Reference equality of value types

I have made some ref keyword tests and there is one thing I can't understand: static void Test(ref int a, ref int b) { Console.WriteLine(Int32.ReferenceEquals(a,b)); } static void Main(string[] args) { int a = 4; Test(ref a, ref a); …
amergan
  • 297
  • 2
  • 4
27
votes
4 answers

Comparing primitive to wrapper object with == behaviour unexplained

I have a piece of code which I need to understand: public static void main(String[] args) { Character c = new Character('a'); Character cy = new Character('a'); char cx = 'a'; System.out.println(c == cx); System.out.println(cx…
Fr_nkenstien
  • 1,923
  • 7
  • 33
  • 66
27
votes
1 answer

.NET Tuple and Equals performance

This is something I had not noticed until today. Apparently, the .NET implementation of the much used tuple classes (Tuple, Tuple etc) causes boxing penalties for value types when equality based operations are performed. Here is how the…
nawfal
  • 70,104
  • 56
  • 326
  • 368
1
2
3
39 40