Thursday, October 16, 2025
HomeLanguagesJavaCurrency getCurrencyCode() Method in Java with Examples

Currency getCurrencyCode() Method in Java with Examples

The getCurrencyCode() Method of Currency class in Java is used to retrieve the currency code of this currency which is actually the official ISO 4217 currency code.

Syntax:

CURRENCY.getCurrencyCode()

Parameters: This method does not accept any parameters.

Return Value: This method returns the ISO 4217 currency code of the currency.

Exceptions: The method throws Runtime Error if an invalid code is called.

Below program illustrates the working of getCurrencyCode() method:

Program 1:




// Java Code to illustrate
// getCurrencyCode() method
  
import java.util.*;
  
public class Currency_Demo {
    public static void main(String[] args)
    {
  
        // Creating a currency with the code
        Currency curr_ency
            = Currency.getInstance("INR");
  
        // Getting the currency code
        String currency_code
            = curr_ency.getCurrencyCode();
        System.out.println("Currency Code of India is: "
                           + currency_code);
    }
}


Output:

Currency Code of India is: INR

Program 2:




// Java Code to illustrate
// getCurrencyCode() method
  
import java.util.*;
  
public class Currency_Demo {
    public static void main(String[] args)
    {
  
        // Creating a currency with the code
        Currency curr_ency
            = Currency.getInstance("USD");
  
        // Getting the currency code
        String currency_code
            = curr_ency.getCurrencyCode();
        System.out.println("Currency Code of USA is: "
                           + currency_code);
    }
}


Output:

Currency Code of USA is: USD

Program 3: For an invalid Currency Code.




// Java Code to illustrate getCurrencyCode() method
  
import java.util.*;
  
public class Currency_Demo {
    public static void main(String[] args)
    {
        try {
  
            // Creating a currency with the code
            Currency curr_ency
                = Currency.getInstance("USDA");
  
            // Getting the currency code
            String currency_code
                = curr_ency.getCurrencyCode();
            System.out.println("Invalid Currency Code: "
                               + currency_code);
        }
        catch (Exception e) {
            System.out.println(e);
        }
    }
}


Output:

java.lang.IllegalArgumentException
RELATED ARTICLES

Most Popular

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS