java.util.Currency

The Currency class of java.util package simply a way to represent a currency. Currencies are identified by their ISO 4217 currency codes. This class has been designed so that there’s never more than one Currency instance for any given currency. Because of this design, the Currency class has no no public constructor. Instead, you can obtain a Currency instance using the getInstance methods. Maybe it is better to relate this behavior to the more commonly used class which is the Calendar which follows the same principle.

Users can supersede the Java runtime currency data by means of the system property java.util.currency.data. If this system property is defined then its value is the location of a properties file, the contents of which are key/value pairs of the ISO 3166 country codes and the ISO 4217 currency data respectively. The value part consists of three ISO 4217 values of a currency, i.e., an alphabetic code, a numeric code, and a minor unit. Those three ISO 4217 values are separated by commas. The lines which start with ‘#’s are considered comment lines.

Currency Class Syntax

public final class Currency
extends Object
implements Serializable

Currency Class Compatibility Version

Currency Class is only available Java 1.4

Basic Usage of Currency

The Currency class as part of the java.util package is one of the classes of the java api that is not widely used but it provides a significant addition to the language. Let’s tackle the basics of using the  class. Let’s discuss first how to instantiate a Currency object

Currency currency = Currency.getInstance(Locale.JAPAN);

Basically the declaration of a Currency object is a little bit different to what we are doing on other classes because it is designed in such a way that there’s never more than one Currency instance for any given currency. Thus the only way is to call the getInstance method of the Currency class.

The getInstance() method is overloaded which takes either a Locale or a String. From our code snippet above we have used the Locale.JAPAN, denoting that we want to use the Japan Currency. In order to print what is the current currency we have set on our Currency object we can use the following:

System.out.println(currency.getDisplayName());

The above code snippet would print Japanese Yen . This is a convenient method to display the currency which has being used.

If you are interested in learning other useful methods of the Currency class, please go through below list:

Currency Method Usage Examples

Modifier and Type Method and Description
static Set getAvailableCurrencies()
This method return all available currencies as a Set object
String getCurrencyCode()
Gets the ISO 4217 currency code of this currency.
int getDefaultFractionDigits()
The getDefaultFractionDigits() method of Currency class is use to get the how many digits is an allowable to specified Currency.
String getDisplayName()
This method returns a more sensible value of the currency object like for instance a currency code of NZD, it doesn’t make much sense to display this value but rather its much better to use the getDisplayName which returns New Zealand Dollar for user consumption.
String getDisplayName(Locale locale)
Gets the name that is suitable for displaying this currency for the specified locale.
static Currency getInstance(Locale locale)
Returns the Currency instance for the country of the given locale.
static Currency getInstance(String currencyCode)
Returns the Currency instance for the given currency code.
int getNumericCode()
Returns the ISO 4217 numeric code of this currency.
String getSymbol()
Gets the symbol of this currency for the default DISPLAY locale.
String getSymbol(Locale locale)
Gets the symbol of this currency for the specified locale.
String toString()
Returns the ISO 4217 currency code of this currency.