boxing

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

一笑奈何 提交于 2019-11-27 17:15:29
问题 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 box; } 回答1: No, there is no such method in the JDK. As it's often the case, however, Apache Commons Lang provides such a method. 回答2: Enter Java 8, and you can do following (boxing): int [] ints = ... Integer[] boxedInts = IntStream.of(ints).boxed().toArray(Integer[]:

Why can I not modify the result of an unboxing conversion?

*爱你&永不变心* 提交于 2019-11-27 15:20:11
struct Point { public int x; public int y; } void Main() { Point p; p.x = 1; p.y = 1; Object o = p; ((Point) o).x = 4; // error ((Point) o).x = 5; // error ((Point) o).x = 6; // error p = (Point) o // expect 6 } Why doesn't it compile to ldloc.1 // o unbox Point ldc.i4.4 stfld Point.x Where C++ CLI allows it. For those who don't know, unbox is not required to create a copy of value types , instead it pushes a pointer to the value on to the stack. Only assignment would create a copy. Because of how value types work, the boxed Point is a copy of the original, and "unboxing" it by casting back to

.NET Tuple and Equals performance

夙愿已清 提交于 2019-11-27 13:09:18
This is something I had not noticed until today. Apparently, the .NET implementation of the much used tuple classes ( Tuple<T> , Tuple<T1, T2> etc) causes boxing penalties for value types when equality based operations are performed. Here is how the class is kind of implemented in the framework (source via ILSpy): public class Tuple<T1, T2> : IStructuralEquatable { public T1 Item1 { get; private set; } public T2 Item2 { get; private set; } public Tuple(T1 item1, T2 item2) { this.Item1 = item1; this.Item2 = item2; } public override bool Equals(object obj) { return this.Equals(obj,

How does a generic constraint prevent boxing of a value type with an implicitly implemented interface?

柔情痞子 提交于 2019-11-27 12:29:20
问题 My question is somewhat related to this one: Explicitly implemented interface and generic constraint. My question, however, is how the compiler enables a generic constraint to eliminate the need for boxing a value type that explicitly implements an interface. I guess my question boils down to two parts: What is going on with the behind-the-scenes CLR implementation that requires a value type to be boxed when accessing an explicitly implemented interface member, and What happens with a generic

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

安稳与你 提交于 2019-11-27 10:46:12
问题 Here is a simple generic type with a unique generic parameter constrained to reference types: class A<T> where T : class { public bool F(T r1, T r2) { return r1 == r2; } } The generated IL by csc.exe is : ldarg.1 box !T ldarg.2 box !T ceq So each parameter is boxed before proceeding with the comparison. But if the constraint indicates that "T" should never be a value type, why is the compiler trying to box r1 and r2 ? 回答1: It's required to satisfy the verifiability constraints for the

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

你。 提交于 2019-11-27 10:00:57
问题 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 value and an unboxed value. For example, in the following LinqPad C# expressions, I have faith that o1 is boxed and i1 is not boxed, but I would like a way to test it in code, or, second best, a way to know FOR SURE when looking at any variable or

Why are Python's arrays slow?

谁都会走 提交于 2019-11-27 09:38:27
问题 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 loop, best of 3: 667 ms per loop In [5]: %timeit sum(A) 1 loop, best of 3: 1.41 s per loop In [6]: %timeit sum(L) 1 loop, best of 3: 627 ms per loop In [7]: %timeit sum(A) 1 loop, best of 3: 1.39 s per loop What could be the cause of such a difference?

C# - Issues with boxing / unboxing / typecasting ints. I don't understand

老子叫甜甜 提交于 2019-11-27 09:29:37
I'm having a hard time understanding this. Consider the following example: protected void Page_Load(object sender, EventArgs e) { // No surprise that this works Int16 firstTest = Convert.ToInt16(0); int firstTest2 = (int)firstTest; // This also works object secondTest = 0; int secondTest2 = (int)secondTest; // But this fails! object thirdTest = Convert.ToInt16(0); int thirdtest2 = (int)thirdTest; // It blows up on this line. } The specific error that I get at runtime is Specified cast is not valid. If I QuickWatch (int)thirdTest in Visual Studio, I get a value of Cannot unbox 'thirdTest' as a

Boxing and unboxing: when does it come up?

半腔热情 提交于 2019-11-27 08:06:04
So I understand what boxing and unboxing is. When's it come up in real-world code, or in what examples is it an issue? I can't imagine doing something like this example: int i = 123; object o = i; // Boxing int j = (int)o; // Unboxing ...but that's almost certainly extremely oversimplified and I might have even done boxing/unboxing without knowing it before. Jon Skeet It's much less of an issue now than it was prior to generics. Now, for example, we can use: List<int> x = new List<int>(); x.Add(10); int y = x[0]; No boxing or unboxing required at all. Previously, we'd have had: ArrayList x =

Why doesn't delegate contravariance work with value types?

邮差的信 提交于 2019-11-27 08:06:04
This snippet is not compiled in LINQPad. void Main() { (new[]{0,1,2,3}).Where(IsNull).Dump(); } static bool IsNull(object arg) { return arg == null; } The compiler's error message is: No overload for 'UserQuery.IsNull(object)' matches delegate 'System.Func' It works for a string array, but doesn't work for int[] . It's apparently related to boxing, but I want to know the details. The answer given (that there is no variance involving value types) is correct. The reason covariance and contravariance do not work when one of the varying type arguments is a value type is as follows. Suppose it did