Home »
Java programming language
Java Currency getSymbol() Method with Example
Currency Class getSymbol() method: Here, we are going to learn about the getSymbol() method of Currency Class with its syntax and example.
Submitted by Preeti Jain, on February 12, 2020
Currency Class getSymbol() method
Syntax:
public String getSymbol();
public String getSymbol(Locale lo);
- getSymbol() method is available in java.util package.
- getSymbol() method is used to get the symbol of this Currency for the default locale.
- getSymbol(Locale lo) method is used to get the symbol of this Currency for the given Locale (lo).
- getSymbol() method does not throw an exception at the time of returning symbol.
- getSymbol(Locale lo) method may throw an exception at the time of returning symbol.
NullPointerException: This exception may throw when the given parameter Locale (lo) is null exists.
- These are non-static methods, it is accessible with class object & if we try to access these methods with the class name then we will get an error.
Parameter(s):
-
In the first case, getSymbol(),
- It does not accept any parameter.
-
In the second case, getSymbol(Locale lo)
- Locale lo – represent the locale for whose symbol is to be returned.
Return value:
In both the cases, the return type of the method is String, it gets symbol of this Currency object.
Example:
// Java program is to demonstrate the example of
// getSymbol() method of Currency
import java.util.*;
public class GetSymbolOfCurrency {
public static void main(String args[]) {
// Instantiates a currency with INR code
// for the default locale
Currency c1 = Currency.getInstance("INR");
// Instantiates a currency for the given locale
Locale lo = Locale.US;
Currency c2 = Currency.getInstance(lo);
// By using getSymbol() method is to return
// the symbol of this Currency for the
// default locale
System.out.print("c1.getSymbol(): ");
System.out.println(c1.getSymbol());
// By using getSymbol(lo) method is to return
// the symbol of this Currency for the
// defined locale
System.out.print("c2.getSymbol(lo): ");
System.out.println(c2.getSymbol(lo));
}
}
Output
c1.getSymbol(): ₹
c2.getSymbol(lo): $