Create a zero value of a generic Number subclass
Zero isn't even mentioned in the Number
class. If you must do this, and I suggest avoiding null
s, is perhaps:
public static <T> T coalesce(T a, T b) {
return a==null ? b : a;
}
You could also create a generic interface for handling numbers with features that make sense to your code:
interface NumberOps<T extends Number> {
T zeroIfNull(T value);
}
Is it possible to do this at all?
Not really. For one thing, when value is null, how would method know which Number implementation to return?