Money problem? No more money problem with JSR354 - Java Money and Currency API

It's still the biggest problem to this very day: there lacks a data type representing money in many major programming languages today. This can be clearly seen from the many questions asked on StackOverflow.com, particularly with regards to currency and money. Seeing that many applications today capture and manipulate money, it has become one of, if not, the most common problem developers face today.

What makes representation of money the world's biggest challenge?

Money can be summarized by 3 simplest characteristics:
  • It has a currency.
  • It has an amount (very specific amount)
  • It can be interchangeable to other currencies.
The currency must be valid in the ISO 4217 Currency code (as of the time of writing, the latest ISO 4217:2008 edition). Java has the Currency class but this only represents the ISO 4217 currency by their currency code. It doesn't support the other 2 characteristics mentioned above, nor does it have any supported API to represent all 3 characteristic that defines money.

The biggest challenge with money is currency exchange. Calculations between the same currency is no problem as 2 defined money can be accurately calculated whereas currency exchange can cause a loss of decimal values between the exchange, particularly when rounding amounts. In one of the SO question, I answered that a combination of Currency as well as BigDecimal is the best way to represent money.

Martin Fowler also was (quote) "puzzled" that there was no money representation in major programming language, so he introduced an enterprise pattern called Money Pattern. One of its implementation can be found here. Though it satisfies the Money Pattern, there needed an API to purely handle money.


Java Money API


In the same question mentioned above, I recommended Joda Money for Money representation. I don't have issues with Joda Money but it lacks functionality such as Currency exchange so that leaves developers back to handle the challenge of writing their own exchange providers.

The developers at Credit Suisse decided to tackle and resolve this challenge and provided the specification to Java Community Process group, the JSR 354 - Money and Currency API. This API has been accepted and will be released on the Java 9 SE release under the javax.money package.

I cannot wait for Java SE 9 release and since I am firm believer of Java to have a Money API, I have included it in my projects. If you're also impatient for the official API release in Java, you can head to JavaMoney Github page and download the API as well as the implementation. All you have to do is download the API (javax.money.money-api if using Maven) as well as the Reference Implementation (RI). You can download the JavaMoney Financial Library as its a complete implementation of the API. I suggest that you add the Maven dependency into your Maven project. As soon as Java SE 9 will be released, you can set the scope as "provided". This means that your code will be consistent with future releases of Java.

Here's to the future of Java Money and it's been long overdue. Oh, and since the API will be released in Java SE 9, the API uses Java 8 specific features such as Functional Interfaces, Streams, etc., so better start upgrading your JDK (which you should have done by now).


Comments