Java Reference
In-Depth Information
It's common to put the visibility modifier —
public
or
private
— first, but
you are free to list the modifiers in any order you like. Be aware that there are addition-
al modifiers that you will encounter and need to learn about as you get deeper into the
language.
The
String
type is special in Java. It's really a class type, but syntactically you
can treat it as a primitive type. Java automatically creates a
String
object whenever
you enclose a string of characters within double-quotes (
"..."
). You aren't required
to invoke a constructor, nor to specify the
new
keyword. Yet
String
is a class, and
there are methods in that class that are available to you. One such method is the
re-
place()
method shown at the end of
Listing 1-2
.
Strings are composed of characters. Java's
char
type is a two-byte construct for
storing a single character in Unicode-s UTF-16 encoding. You can generate literals of
the
char
type in two ways:
•
If a character is easy to type, then enclose it within single quotes (e.g.:
'G'
).
•
Otherwise, specify the four-digit UTF-16
code point
value prefaced by
\u
(e.g.:
'\u0490'
).
Some Unicode code points require five digits. These cannot be represented in a
nationalization.
Avoid using any of the primitive types for monetary values. Especially avoid either
of the floating-point types for that purpose. Refer instead to
Chapter 4
and its recipe on
using
BigDecimal
to calculate monetary amounts (Recipe 4-7).
BigDecimal
is
also useful anytime you need accurate, fixed-decimal arithmetic.
If you are new to Java, you may be unfamiliar with the
String[]
array notation,
as demonstrated in the examples. Please see
Chapter 7
for more information on arrays.
It covers enumerations, arrays, and also generic data types. Also in that chapter are ex-
amples showing how to write iterative code to work with collections of values such as
an array.
Note
If you're curious about the Ukrainian letter in
Listing 1-2
, it is the Cyrillic let-
ter
Ghe with upturn
. You can read about its history at:
ht-
tp://en.wikipedia.org/wiki/Ghe_with_upturn
. You can find its code
point value in the chart at
http://www.unicode.org/charts/PDF/