next up previous
Next: 1.2.5 The Structure of Up: 1.2 Java Mechanics Previous: 1.2.3 Precedence of Operations

  
1.2.4 Java Statements

Since the Java expression language is not very rich, Java programs express computations as sequences of statements rather than as compound expressions. The most common form of Java statement is an assignment statement
type var $\,$ = expr ;
where type is a Java type name, var is a Java variable name, and expr is an expression of type compatible with the type of var. The assignment statement
int x = 5;
asserts that ``the variable x has value 5''.

Java variable names and type names are identifiers consisting of any sequence of alphanumeric characters (letters, digits, and _) beginning with a letter or _ other than one of the following keywords:

abstract    default     if          private     throw
boolean     do          implements  protected   throws
break       double      import      public      transient
byte        else        instanceof  return      try
case        extends     int         short       void
catch       final       interface   static      volatile
char        finally     long        super       while
class       float       native      switch
const       for         new         synchronized
continue    goto        package     this
Java is case-sensitive; the variable X is distinct from the variable x. There are three kinds of variables in Java: fields, method parameters, and local variables. Fields and method parameters are discussed in detail in the next subsection. We will defer a discussion of local variables until Section 1.11.1.

Java includes all of the basic statement forms found in the C programming language expressed in essentially the same syntax. In the remainder of this monograph, we will introduce these statement forms as they are needed. Although Java accepts most C syntax, many common C constructions are considered bad style in Java. For example, an assignment without the trailing semicolon is technically a valid expression in both C and Java. Common usage in C exploits this fact, embedding assignments as subexpressions of other expressions. In Java, this coding practice is considered very bad style and will not be mentioned again in this monograph.


next up previous
Next: 1.2.5 The Structure of Up: 1.2 Java Mechanics Previous: 1.2.3 Precedence of Operations
Corky Cartwright
2000-01-07