boxing

Widening and Boxing Java primitives

风格不统一 提交于 2019-12-18 08:09:11
问题 Widening and Boxing Java primitives. I know it is not possible to widen a wrapper class from one to another as they are not from the same inheritence tree. Why though is it not possible to widen a primitive to another primitive type and autobox the widened primitive? Given that a byte argument can be passed to a method that expects an int, why cant the byte in the following example be widened to an int and then boxed to an Integer? class ScjpTest{ static void goInteger(Integer x){ System.out

Can I set a value on a struct through reflection without boxing?

纵然是瞬间 提交于 2019-12-18 07:40:50
问题 Actually, I should've asked: how can I do this and remain CLS Compliant? Because the only way I can think of doing this is as follows, but using either __makeref , FieldInfo.SetValueDirect or just System.TypedReference in general invalidates CLS Compliance. // code illustrating the issue: TestFields fields = new TestFields { MaxValue = 1234 }; // test struct with one field FieldInfo info = fields.GetType().GetField("MaxValue"); // get the FieldInfo // actual magic, no boxing, not CLS

How does the mechanism behind the creation of boxed traits work?

孤者浪人 提交于 2019-12-18 06:54:06
问题 I'm having trouble understanding how values of boxed traits come into existence. Consider the following code: trait Fooer { fn foo(&self); } impl Fooer for i32 { fn foo(&self) { println!("Fooer on i32!"); } } fn main() { let a = Box::new(32); // works, creates a Box<i32> let b = Box::<i32>::new(32); // works, creates a Box<i32> let c = Box::<Fooer>::new(32); // doesn't work let d: Box<Fooer> = Box::new(32); // works, creates a Box<Fooer> let e: Box<Fooer> = Box::<i32>::new(32); // works,

boxing and unboxing in int and string

梦想与她 提交于 2019-12-18 04:12:40
问题 I am little bit confused in boxing and unboxing. According to its definition Boxing is implicit conversion of ValueTypes to Reference Types (Object). UnBoxing is explicit conversion of Reference Types (Object) to its equivalent ValueTypes. the best example for describing this is int i = 123; object o = i; // boxing and o = 123; i = (int)o; // unboxing But my question is that whether int is value type and string is reference type so int i = 123; string s = i.ToString(); and s = "123"; i = (int

How do I get an IntStream from a List<Integer>?

点点圈 提交于 2019-12-17 18:44:56
问题 I can think of two ways: public static IntStream foo(List<Integer> list) { return list.stream().mapToInt(Integer::valueOf); } public static IntStream bar(List<Integer> list) { return list.stream().mapToInt(x -> x); } What is the idiomatic way? Maybe there is already a library function that does exactly what I want? 回答1: I guess (or at least it is an alternative) this way is more performant: public static IntStream baz(List<Integer> list) { return list.stream().mapToInt(Integer::intValue); }

Java signed zero and boxing

余生颓废 提交于 2019-12-17 18:27:44
问题 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 Double(0.0).equals(new Double(-0.0)) evaluates to false . Does anyone know the reason behind this? 回答1: It is all explained in the javadoc: Note that in most cases, for two instances of class Double, d1 and d2, the value of d1.equals(d2) is true if

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

拥有回忆 提交于 2019-12-17 17:25:07
问题 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>(TEnum value) where TEnum : struct // C# does not allow enum constraint { return (int) (ValueType) value; } The above C# is release-mode compiled to the following IL (note boxing and unboxing opcodes): .method public hidebysig instance int32 Foo<valuetype .ctor ([mscorlib

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:

Compiler error : reference to call ambiguous

梦想的初衷 提交于 2019-12-17 06:47:14
问题 Case 1 static void call(Integer i) { System.out.println("hi" + i); } static void call(int i) { System.out.println("hello" + i); } public static void main(String... args) { call(10); } Output of Case 1 : hello10 Case 2 static void call(Integer... i) { System.out.println("hi" + i); } static void call(int... i) { System.out.println("hello" + i); } public static void main(String... args) { call(10); } Shows compilation error reference to call ambiguous . But, I was unable to understand. Why ? But