Java: Generic methods and numbers

There are ways you can hack this together but in all honestly, generics is simply not the way to go here. Build a method for each concrete primitive wrapper type and implement them separately. It’ll be way too much of a headache to make it generic; arithmetic operations can’t happen generically.

You don’t really gain anything by making it generic, either. It’s such simple and constant code that you aren’t worried about code duplication, since it’s not going to change. And people aren’t going to be passing in their own type of Number to your code; the domain of types it applies to is already well defined and finite.

Leave a Comment