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?
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?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With