• Java programs, which consist of one or more interacting classes that may be
drawn from one or more packages.
The syntax of most programming languages is complex, and Java is no exception. In
general, it is not possible to document all elements of a language without referring
to other elements that have not yet been discussed. For example, it is not really pos‐
sible to explain in a meaningful way the operators and statements supported by Java
without referring to objects. But it is also not possible to document objects thor‐
oughly without referring to the operators and statements of the language. The pro‐
cess of learning Java, or any language, is therefore an iterative one.
Java Programs from the Top Down
Before we begin our bottom-up exploration of Java syntax, let's take a moment for a
top-down overview of a Java program. Java programs consist of one or more files, or
compilation units , of Java source code. Near the end of the chapter, we describe the
structure of a Java file and explain how to compile and run a Java program. Each
compilation unit begins with an optional package declaration followed by zero or
more import declarations. These declarations specify the namespace within which
the compilation unit will define names, and the namespaces from which the compi‐
lation unit imports names. We'll see package and import again later in this chapter
in “Packages and the Java Namespace” on page 88 .
The optional package and import declarations are followed by zero or more refer‐
ence type definitions. We will meet the full variety of possible reference types in
Chapters 3 and 4 , but for now, we should note that these are most often either class
or interface definitions.
Within the definition of a reference type, we will encounter members such as ields ,
methods , and constructors . Methods are the most important kind of member. Meth‐
ods are blocks of Java code comprised of statements .
With these basic terms defined, let's start by approaching a Java program from the
bottom up by examining the basic units of syntax—often referred to as lexical
This section explains the lexical structure of a Java program. It starts with a discus‐
sion of the Unicode character set in which Java programs are written. It then covers
the tokens that comprise a Java program, explaining comments, identifiers, reserved
words, literals, and so on.
The Unicode Character Set
Java programs are written using Unicode. You can use Unicode characters any‐
where in a Java program, including comments and identifiers such as variable
names. Unlike the 7-bit ASCII character set, which is useful only for English, and