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
564
votes
21 answers

How to convert int[] into List in Java?

How do I convert int[] into List in Java? Of course, I'm interested in any other answer than doing it in a loop, item by item. But if there's no other answer, I'll pick that one as the best to show the fact that this functionality is not…
Pablo Fernandez
  • 279,434
  • 135
  • 377
  • 622
389
votes
12 answers

Why do we need boxing and unboxing in C#?

Why do we need boxing and unboxing in C#? I know what boxing and unboxing is, but I can't comprehend the real use of it. Why and where should I use it? short s = 25; object objshort = s; //Boxing short anothershort = (short)objshort; //Unboxing
Vaibhav Jain
  • 33,887
  • 46
  • 110
  • 163
163
votes
5 answers

Why are Python's arrays slow?

I expected array.array to be faster than lists, as arrays seem to be unboxed. However, I get the following result: In [1]: import array In [2]: L = list(range(100000000)) In [3]: A = array.array('l', range(100000000)) In [4]: %timeit sum(L) 1…
Valentin Lorentz
  • 9,556
  • 6
  • 47
  • 69
152
votes
9 answers

What is boxing and unboxing and what are the trade offs?

I'm looking for a clear, concise and accurate answer. Ideally as the actual answer, although links to good explanations welcome.
Keith
  • 150,284
  • 78
  • 298
  • 434
149
votes
17 answers

Convert an array of primitive longs into a List of Longs

This may be a bit of an easy, headdesk sort of question, but my first attempt surprisingly completely failed to work. I wanted to take an array of primitive longs and turn it into a list, which I attempted to do like this: long[] input =…
Brandon Yarbrough
  • 37,021
  • 23
  • 116
  • 145
93
votes
9 answers

How to convert byte[] to Byte[] and the other way around?

How to convert byte[] to Byte[] and also Byte[] to byte[], in the case of not using any 3rd party library? Is there a way to do it fast just using the standard library?
user926958
  • 9,355
  • 7
  • 28
  • 33
90
votes
5 answers

Boxing Occurrence in C#

I'm trying to collect all of the situations in which boxing occurs in C#: Converting value type to System.Object type: struct S { } object box = new S(); Converting value type to System.ValueType type: struct S { } System.ValueType box = new…
controlflow
  • 6,667
  • 1
  • 31
  • 55
87
votes
6 answers

What is the difference between boxing/unboxing and type casting?

What is the difference between boxing/unboxing and type casting? Often, the terms seem to be used interchangeably.
Faith
86
votes
7 answers

Why comparing Integer with int can throw NullPointerException in Java?

It was very confusing to me to observe this situation: Integer i = null; String str = null; if (i == null) { //Nothing happens ... } if (str == null) { //Nothing happens } if (i == 0) { //NullPointerException ... } if…
Roman
  • 64,384
  • 92
  • 238
  • 332
76
votes
9 answers

C# non-boxing conversion of generic enum to int?

Given a generic parameter TEnum which always will be an enum type, is there any way to cast from TEnum to int without boxing/unboxing? See this example code. This will box/unbox the value unnecessarily. private int Foo(TEnum value) where…
Jeff Sharp
  • 979
  • 1
  • 9
  • 10
74
votes
6 answers

Boxing and unboxing with generics

The .NET 1.0 way of creating collection of integers (for example) was: ArrayList list = new ArrayList(); list.Add(i); /* boxing */ int j = (int)list[0]; /* unboxing */ The penalty of using this is the lack of type safety and performance…
Itay Karo
  • 17,924
  • 4
  • 40
  • 58
64
votes
2 answers

How do I get an IntStream from a List?

I can think of two ways: public static IntStream foo(List list) { return list.stream().mapToInt(Integer::valueOf); } public static IntStream bar(List list) { return list.stream().mapToInt(x -> x); } What is the idiomatic…
fredoverflow
  • 256,549
  • 94
  • 388
  • 662
61
votes
5 answers

In Java 8, is there a ByteStream class?

Java 8 provides Stream specializations for double, int and long: DoubleStream, IntStream and LongStream respectively. However, I could not find an equivalent for byte in the documentation. Does Java 8 provide a ByteStream class?
sdgfsdh
  • 33,689
  • 26
  • 132
  • 245
58
votes
4 answers

Structs, Interfaces and Boxing

Possible Duplicate: Is it safe for structs to implement interfaces? Take this code: interface ISomeInterface { public int SomeProperty { get; } } struct SomeStruct : ISomeInterface { int someValue; public int SomeProperty { get {…
Sekhat
  • 4,435
  • 6
  • 43
  • 50
52
votes
4 answers

Does autoboxing call valueOf()?

I'm trying to determine whether the following statements are guaranteed to be true: ((Boolean)true) == Boolean.TRUE ((Boolean)true) == Boolean.valueOf(true) ((Integer)1) == Integer.valueOf(1) I've always assumed that autoboxing was equivalent to…
shmosel
  • 49,289
  • 6
  • 73
  • 138
1
2 3
39 40