Java Reference
In-Depth Information
which are discussed later in this chapter. Still others are used to define classes and
their members (see
Chapter 3
).
Note that
const
and
goto
are reserved but aren't actually used in the language, and
that
interface
has an additional variant form—
@interface
, which is used when
defining types known as annotations. Some of the reserved words (notably
final
and
default
) have a variety of different meanings depending on context.
a
x
Identiiers
An
identiier
is simply a name given to some part of a Java program, such as a class,
a method within a class, or a variable declared within a method. Identifiers may be
of any length and may contain letters and digits drawn from the entire Unicode
character set. An identifier may not begin with a digit. In general, identifiers may
not contain punctuation characters. Exceptions include the ASCII underscore (
_
)
and dollar sign (
$
) as well as other Unicode currency symbols such as
£
and
¥
.
Currency symbols are intended for use in automatically gener‐
ated source code, such as code produced by
javac
. By avoid‐
ing the use of currency symbols in your own identifiers, you
don't have to worry about collisions with automatically gener‐
ated identifiers.
Formally, the characters allowed at the beginning of and within an identifier are
defined by the methods
isJavaIdentifierStart()
and
isJavaIdentifierPart()
of the class
java.lang.Character
.
The following are examples of legal identifiers:
i
x1
theCurrentTime
the_current_time
獺
Note in particular the example of a UTF-8 identifier—
獺
. This is the Kanji character
for “otter” and is perfectly legal as a Java identifier. The usage of non-ASCII identifi‐
ers is unusual in programs predominantly written by Westerners, but is sometimes
seen.
Literals
Literals are values that appear directly in Java source code. They include integer and
floating-point numbers, single characters within single quotes, strings of characters
within double quotes, and the reserved words
true
,
false
, and
null
. For example,
the following are all literals:
1
1.0
'1'
"one"
true
false
null
The syntax for expressing numeric, character, and string literals is detailed in
“Primitive Data Types” on page 22
.