This is why nobody likes Java

Recently, I wanted to pass a random number into a unit test. Sounds simple, right? It probably would be if I wasn’t writing it in Java.

The problem is that the class expected a BigDecimal. But the random utils returns a string. And you can’t convert a string to a BigDecimal. So I had to convert it to a Long, and then convert that to a BigDecimal. Here is the code I ended up with:

BigDecimal pageNumber = BigDecimal.valueOf(Long.valueOf(RandomStringUtils.randomNumeric(1)));

Which started me wondering: how many ways to represent a number are there in Java? So, I looked it up. And came up with this list:

  1. AtomicInteger
  2. AtomicLong
  3. BigDecimal
  4. BigInteger
  5. Byte
  6. double
  7. Double
  8. float
  9. Float
  10. int
  11. Integer
  12. long
  13. Long
  14. short
  15. Short

Some of these are understandable. It makes sense to have separate storage for decimals and integers, for example. But do we really need a short and a Short? And a total of 15 different types of number? It’s madness.

It wouldn’t be so bad if you could just compare the two of them. Or pass in a number to a function. But it is a strongly typed language. Which means a world of pain when people use different types.

But that is what you get for trying to use a proper language, I guess.

Comments
Free newsletter

Don't have time to check my blog? Get a weekly email with all the new posts. This is my personal blog, so obviously it is 100% spam free.

Metadata

Tags:

This entry was posted on Saturday, June 17th, 2017 at 11:00 am and is filed under Programming. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.