Java Reference
In-Depth Information
Some constants for other countries (and hence other currencies) are given in Display
2.4. However, unless your screen is capable of displaying the currency symbol for the
country whose constant you use, the output might not be as desired.
These location constants are objects of the class
Locale
. In order to make the class
Locale
and these constants available to your code, you must include the following
near the start of the file with your program:
import
java.util.Locale;
If you do not use any of these location constants and only use the default location, you
do not need this
import
statement.
The notation
Locale.US
may seem a bit strange, but it follows a convention that is
frequently used in Java code. The constant is named
US,
but we want specifically that
constant named
US
that is defined in the class
Locale
. So we use
Locale.US
. The nota-
tion
Locale.US
means the constant
US
as defined in the class
Locale
.
Importing Packages and Classes
Libraries in Java are called
packages
. A package is simply a collection of classes that
has been given a name and stored in such a way as to make it easily accessible to your
Java programs. Java has a large number of standard packages that automatically come
with Java. Two such packages are named
java.text
and
java.util
. In Display 2.3
we used the class
NumberFormat
, which is a member of the package
java.text
. In
order to use
NumberFormat
, you must import the class, which we did as follows:
package
import
java.text.NumberFormat;
java.text
Display 2.4
Locale
Constants for Currencies of Different Countries
Locale.CANADA
Canada (for currency, the format is the same as US)
Locale.CHINA
China
Locale.FRANCE
France
Locale.GERMANY
Germany
Locale.ITALY
Italy
Locale.JAPAN
Japan
Locale.KOREA
Korea
Locale.TAIWAN
Taiwan
Locale.UK
United Kingdom (English pound)
Locale.US
United States