Menu
  • HOME
  • TAGS

In Java, is it possible to override methods if return types are respectively a primitive and its wrapper class?

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...

NullPointerException throws when I use ternary operator [duplicate]

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...

foreach loop by primitive or by boxed class in Java

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...

Unboxing -1 and casting to Nullable using generics yields InvalidCastException

c#,generics,casting,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...

Unbox a number to double

c#,.net,boxing,unboxing

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,...

Efficient alternative to Map in Java, with regards to autoboxing?

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,...

Is casting from Number to double allowed in Java 7? (Autoboxing)

java,eclipse,unboxing

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...

Boxing and unboxing is a myth?

c#,clr,boxing,unboxing

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...

Why are casting and conversion operations are syntactically indistinguishable?

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...

Helping GHC unbox an Int in equality on a constant

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...

Unboxing Integer[] in AsyncTask

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...

Generics supports only reference conversions not boxing conversions

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[]...

Method overload resolution in java

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 unboxing in C#

c#,boxing,unboxing

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...