3 ms·
The problem is that the reason people need value types is often for performance - once you've busted out value types for performance, you expect to be able to k
by Pxtl 13y ago
The problem is that the reason people need value types is often for performance - once you've busted out value types for performance, you expect to be able to keep it with generics, and that's where boxing/unboxing becomes apparent.
Unboxing can also produce counter-intuitive typecast behavior.
If I want to cast an object-boxed int into a short
Object myBoxedInt = GetInt();
short myShort = (short)(int)myIntObj;
because casting directly to short is a run-time exception. I've run into this problem in C#, but my understanding is that Java shares it.
- bradleyjg 13y agoThe java boxed Integer class has you covered: Integer boxed = ...; short myShort = boxed.shortValue();