unboxing

Box and UnBox what does it means? [duplicate]

☆樱花仙子☆ 提交于 2019-12-21 03:51:42
问题 This question already has answers here : Closed 8 years ago . Possible Duplicates: Why do we need boxing and unboxing in C#? What is boxing and unboxing and what are the trade offs? In C# what doe sit means: "Box and UnBox"? Here an extract from MSDN where I founded the Text. But this convenience comes at a cost. Any reference or value type that is added to an ArrayList is implicitly upcast to Object. If the items are value types, they must be boxed when they are added to the list, and

Boxing vs Unboxing

守給你的承諾、 提交于 2019-12-20 09:33:17
问题 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 to calculate this: int i = 20; object j = i; j = 50; What is i ? I messed it up and said 50, where its actually 20. Now I think understand it why, however when I was playing with different combinations I was surprised to see this: Object a = 1; //

Why do some languages need Boxing and Unboxing?

馋奶兔 提交于 2019-12-20 08:53:28
问题 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<double> dummy; I have some experience with Java, but I was really surprised because I had to write something like this, ArrayList<Double> dummy = new ArrayList<Double>(); My question, why should it be an Object, what is so hard technically to include primitive types

Why does autoboxing in Java allow me to have 3 possible values for a boolean?

倾然丶 夕夏残阳落幕 提交于 2019-12-17 16:28:51
问题 Reference: http://java.sun.com/j2se/1.5.0/docs/guide/language/autoboxing.html "If your program tries to autounbox null, it will throw a NullPointerException." javac will give you a compile-time error if you try to assign null to a boolean. makes sense. assigning null to a Boolean is a-ok though. also makes sense, i guess. but let's think about the fact that you'll get a NPE when trying to autounbox null. what this means is that you can't safely perform boolean operations on Booleans without

Boxing and unboxing with generics

狂风中的少年 提交于 2019-12-17 15:21:51
问题 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 due to boxing and unboxing. The .NET 2.0 way is to use generics: List<int> list = new List<int>(); list.Add(i); int j = list[0]; The price of boxing (to my understanding) is the need to create an object on the heap, copy the stack allocated integer to the new

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

倾然丶 夕夏残阳落幕 提交于 2019-12-17 13:41:25
问题 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. 回答1:

Method overload resolution in java

非 Y 不嫁゛ 提交于 2019-12-17 06:42:52
问题 Here is what I know about overload resolution in java: The process of compiler trying to resolve the method call from given overloaded method definitions is called overload resolution. If the compiler can not find the exact match it looks for the closest match by using upcasts only (downcasts are never done). Here is a class: public class MyTest { public static void main(String[] args) { MyTest test = new MyTest(); Integer i = 9; test.TestOverLoad(i); } void TestOverLoad(int a){ System.out

Why can't I unbox an int as a decimal?

最后都变了- 提交于 2019-12-17 01:01:46
问题 I have an IDataRecord reader that I'm retrieving a decimal from as follows: decimal d = (decimal)reader[0]; For some reason this throws an invalid cast exception saying that the "Specified cast is not valid." When I do reader[0].GetType() it tells me that it is an Int32. As far as I know, this shouldn't be a problem.... I've tested this out by this snippet which works just fine. int i = 3750; decimal d = (decimal)i; This has left me scratching my head wondering why it is failing to unbox the

Generics with autoboxing and unboxing of primitives

独自空忆成欢 提交于 2019-12-14 03:37:35
问题 Why autoboxing and unboxing of primitives not happens with Generics Java. public static <T extends Number> T addNumber(T a , T b) { int c = a*b; System.out.println(c); return c; } Here why * operation can't be performed and why can't return c.Any help would be appreciable. 回答1: Generics are not supposed to be used with primitive types. T indicates a type parameter which should be an object. More reference Why don't Java Generics support primitive types? Java Generics ? , E and T what is the

Are arrays being transformed when using an enhanced for loop?

北城余情 提交于 2019-12-13 18:36:46
问题 Does Java 5 or higher apply some of form of "boxing" to arrays? This question came to mind as the following code goes through an array as if it's an Iterable. for( String : args ){ // Do stuff } 回答1: No, arrays are always reference types. There's no need for boxing or unboxing, unless it's on the access for each element. For example: int[] x = new int[10]; // The value of x is a reference int y = x[0]; // No boxing Integer z = x[1]; // Boxing conversion from x[1] (which is an int) to Integer