2.1. Java Programs from the Top DownBefore 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 compilation unit imports names. We'll see package and import again in Section 2.10 later in this chapter. The optional package and import declarations are followed by zero or more reference type definitions. These are typically class or interface definitions, but in Java 5.0 and later, they can also be enum definitions or annotation definitions. The general features of reference types are covered later in this chapter, and detailed coverage of the various kinds of reference types is in Chapters Chapter 3 and Chapter 4. Type definitions include members such as fields, methods, and constructors. Methods are the most important type member. Methods are blocks of Java code comprised of statements. Most statements include expressions, which are built using operators and values known as primitive data types. Finally, the keywords used to write statements, the punctuation characters that represent operators, and the literals values that appear in a program are all tokens, which are described next. As the name of this section implies, this chapter moves from describing the smallest units, tokens, to progressively larger units. Since the concepts build upon one another, we recommend reading this chapter sequentially. |