How can I generically create a zero of an arbitrary numeric type?
Here's a toy example: a function that converts a null number into zero.
static <T extends Number> T zeroIfNull(T value) {
return value == null ? 0 : value;
}
This doesn't compile because the literal zero is of type int
, and I need to convert that to type T
.
Is it possible to do this at all?