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.

551 questions
444
votes
20 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…
pupeno
  • 256,034
  • 114
  • 324
  • 541
352
votes
11 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
  • 31,558
  • 43
  • 103
  • 159
154
votes
4 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
  • 8,859
  • 5
  • 42
  • 63
141
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
  • 33,119
  • 22
  • 98
  • 134
137
votes
8 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
  • 133,927
  • 68
  • 273
  • 391
86
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,530
  • 29
  • 54
84
votes
7 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
  • 8,801
  • 7
  • 24
  • 32
83
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
  • 59,060
  • 84
  • 230
  • 322
77
votes
5 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
71
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
  • 16,882
  • 3
  • 36
  • 56
69
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
  • 900
  • 1
  • 9
  • 10
52
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
  • 24,047
  • 15
  • 89
  • 182
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
  • 42,915
  • 5
  • 56
  • 120
52
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,359
  • 6
  • 37
  • 49
52
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
  • 237,063
  • 85
  • 359
  • 638
1
2 3
36 37