java,autoboxing,method-overriding,unboxing
You can only return same type or a sub-class of the parent's return type and its known as Co-Variant return types. The compiler lets you auto-box and unbox between primitives and wrappers but this doesn't make one a sub-class of the other. Primitives are not classes and cannot be used...
java,boolean,ternary-operator,boxing,unboxing
You guessed it right. For a formal explanation, the answer lies in the JLS: If one of the second and third operands is of primitive type T, and the type of the other is the result of applying boxing conversion (ยง5.1.7) to T, then the type of the conditional expression...
java,generics,collections,boxing,unboxing
Boxing/unboxing has a cost at runtime. if you are going to unbox the Integer once within the body of your loop, like you do, it won't make much of a difference. if you unbox the Integer many times in the body of your loop, you would be better off unboxing...
For a really good answer, you need to post a good, minimal, complete code example. If a cast to int fails, then the boxed value isn't an int. You should be able to see in the debugger what it actually is. That said, the Convert class is a lot more...
The simplest way is probably to use Convert.ToDouble. This does the conversion for you, and works with numeric types, strings, and anything else that implements IConvertible (and has a value that can be converted to a double). public static double Foo(object obj) { // you could include a check (IsValueType,...
java,android,linkedhashmap,autoboxing,unboxing
Efficient alternative to Map ? SparseIntArrays is more efficient than HashMap<Integer,Integer>. According to documentation SparseIntArrays map integers to integers. Unlike a normal array of integers, there can be gaps in the indices. It is intended to be more memory efficient than using a HashMap to map Integers to Integers,...
With Java 7, the casting system had to be changed slightly with regards to primitive types in order to allow working with MethodHandles. When invoking a method handle, the javac compiler generates a so-called polymorhic signature which derives from the method handle's method signatures. These polymorphic signatures are created by...
There is a particular memory layout for Object instances. To save space and for compatibility with native code (COM, p/invoke, etc), instances of value types do not conform to that layout. "boxing" embeds the value inside an actual "object" instance with the expected layout. This enables polymorphic use by all...
c#,.net,casting,type-conversion,unboxing
Your observation is, as far as I can tell, the observation that I made here: http://ericlippert.com/2009/03/03/representation-and-identity/ There are two basic usages of the cast operator in C#: (1) My code has an expression of type B, but I happen to have more information than the compiler does. I claim to...
haskell,optimization,ghc,unboxing
On my Ubuntu box, the relevant core code compiled to this 406168: 48 81 7b 07 3f 42 0f cmpq $0xf423f,0x7(%rbx) 40616f: 00 406170: 75 28 jne 40619a <c4fz_info+0x32> 406172: bf 52 e3 6d 00 mov $0x6de352,%edi 406177: be 90 65 6d 00 mov $0x6d6590,%esi 40617c: 41 be 18 6a...
android,amazon-web-services,android-asynctask,autoboxing,unboxing
There's no unboxing needed. Refer to Arbitrary Number of Arguments to see that when you say that an argument is of type Integer... what you're actually working with inside the method is Integer[] even if we just pass one single item it will be inside an array, so you can...
c#,generics,boxing,variance,unboxing
Well, there's a reference conversion between string and object, in that every string reference can be treated as an object reference. This can be done transparently, with no modification to the value at all. That's why array variance can be done cheaply - for reads, anyway. Reading from an object[]...
java,method-overloading,unboxing
The compiler will consider not a downcast, but an unboxing conversion for overload resolution. Here, the Integer i will be unboxed to an int successfully. The String method isn't considered because an Integer cannot be widened to a String. The only possible overload is the one that considers unboxing, so...
Boxing is the process of converting a value type to the type object or to any interface type implemented by this value type. When the CLR boxes a value type, it wraps the value inside a System.Object and stores it on the managed heap. Unboxing extracts the value type...