This is NOT homework.
Part 1
Is it possible to write a generic method, something like this:
<T extends Number> T plusOne(T num) {
return num + 1; // DOESN'T COMPILE! How to fix???
}
Short of using a bunch of instanceof
and casts, is this possible?
Part 2
The following 3 methods compile:
Integer plusOne(Integer num) {
return num + 1;
}
Double plusOne(Double num) {
return num + 1;
}
Long plusOne(Long num) {
return num + 1;
}
Is it possible to write a generic version that bound T
to only Integer
, Double
, or Long
?
Arithmetic operations in Java work only on primitives. You here are combining generics and autoboxing unboxing etc.
For such a simple case as yours I'll suggest use only primitives.
The problem here is that your code must unbox the object, operate on the primitive, and then rebox it. Java really can't do that because by the time the code is compiled, it doesn't know what the type is any more, so it doesn't know how to unbox.
The value of Java generics is really to preserve type safety, i.e. the compiler knows the real class and will prevent illegal assignments. The compiler will NOT generate different code depending on the type: it WON'T say "oh, that's an integer, so I need to generate an integer add here, versus that one's a String, so the plus sign really means string concatenation". It's really quite different from C++ templates, if that's what you're thinking of.
The only way you could make this work would be if there was a plusOne function defined for Number, which there isn't.
Part 1
There is no satisfactory solution for this, since
java.lang.Number
doesn't specify anything that would be useful to compute the successor of aNumber
.You'd have to do
instanceof
checks for the numeric box types, and handle each case specially. Note also that you may get aninstanceof Number
that's none of the numeric box types, e.g.BigInteger
,AtomicLong
, and potentially unknown subclasses ofNumber
(e.g.Rational
, etc).Part 2
Look is very deceiving, here. The 3 methods may look alike, but autoboxing/unboxing hides the fact that they're actually very different at the bytecode level:
Not only are the 3 methods invoking different
xxxValue()
andvalueOf()
methods on different types, but the instruction to push the constant1
to the stack is also different (iconst_1
,dconst_1
, andlconst_1
).Even if it's possible to bind a generic type like
<T=Integer|Long|Double>
, the 3 methods are not genericizable into one method since they contain very different instructions.Not the prettiest solution ever, but if you rely in the following properties of every known implementation of Number (in the JDK):
You can implement it using reflection and using Generics to avoid having to cast the result:
The return is done using an apparently unsafe cast but given that you're using a constructor of the class of some T or child of T you can assure that it will always be a safe cast.
Not all of the subclasses of Number can be autounboxed. BigDecimal, for instance, can't be autounboxed. Therefore the "+" operator won't work for it.
Part 1:
Doesn't
num + 1
work without the need to create such method? The+
operator is overloaded just for that. That is, why call:when you can do:
The bottomline is - you can't combine autoboxing with generics.