The JavaTM Tutorial

| Start of Tutorial > Start of Trail > Start of Lesson | Search

Trail: Learning the Java Language
Lesson: The Nuts and Bolts of the Java Language

## Expressions, Statements, and Blocks

> [PENDING: this could use some tightening of the language and cleanup]

### Expressions

> Expressions perform the work of a Java program. Among other things, expressions are used to compute and assign values to variables and to help control the execution flow of a program. The job of an expression is two-fold: perform the computation indicated by the elements of the expression and return some value that is the result of the computation.

> > Definition: An expression is a series of variables, operators, and method calls (constructed according to the syntax of the language) that evaluates to a single value.

> As discussed in the previous section, operators return a value, so the use of an operator is an expression. This partial listing of the `MaxVariables` program shows some of the program's expressions in red:

> >     ...
> >     // other primitive types
> >     char aChar = 'S';
> >     boolean aBoolean = true;

> >     // display them all
> >     System.out.println("The largest byte value is " + largestByte);
> >     ...

> >     if (Character.isUpperCase(aChar)) {
> >         ...
> >     }

> Each of these expressions perform an operation and return a value.

> Expression | Action | Value Returned
> --- | --- | ---
> `aChar = 'S'` | Assign the character 'S' to the character variable `aChar` | The value of `aChar` after the assignment ('S')
> `"The largest byte value is " + largestByte` | Concatenate the string `"The largest byte value is "` and the value of `largestByte` converted to a string | The resulting string: `The largest byte value is 127`
> `Character.isUpperCase(aChar)` | Call the method `isUpperCase` | The return value of the method: `true`

> The data type of the value returned by an expression depends on the elements used in the expression. The expression `aChar = 'S'` returns a character because the assignment operator returns a value of the same data type as its operands and `aChar` and `'S'` are characters. As you see from the other expressions, an expression can return a boolean value, a string, and so on.

> The Java programming language allows you to construct compound expressions and statements from various smaller expressions as long as the data types required by one part of the expression matches the data types of the other. Here's an example of a compound expression:

> >     x * y * z

> In this particular example, the order in which the expression is evaluated is unimportant because the results of multiplication is independent of order--the outcome is always the same no matter what order you apply the multiplications. However, this is not true of all expressions. For example, the following expression gives different results depending on whether you perform the addition or the division operation first:

> >     x + y / 100

> You can direct the Java compiler explicitly how you want an expression evaluated with balanced parentheses `(` and `)`. For example to make the previous expression unambiguous, you could write `(x + y)/ 100`.

> If you don't explicitly tell the compiler the order in which you want operations to be performed, it decides based on the precedence assigned to the operators and other elements in use within the expression. Operators with a higher precedence get evaluated first. For example, the division operator has a higher precedence than does the addition operator so, in the compound expression shown previously, `x + y / 100`, the compiler would evaluate `y / 100` first. Thus

> >     x + y / 100

> is equivalent to

> >     x + (y / 100)

> To make your code easier to read and maintain you should be explicit and indicate with parentheses which operators should be evaluated first.

> The following table shows the precedence assigned to Java's operators. The operators in this table are listed in precedence order: the higher in the table an operator appears, the higher its precedence. Operators with higher precedence are evaluated before operators with a relatively lower precedence. Operators on the same line have equal precedence.

> #include tables/precedence.mem4

> When operators of equal precendence appear in the same expression, some rule must govern which is evaluated first. In Java, all binary operators except for the assignment operators are evaluated in left to right order. Assignment operators are evaluated right to left.

### Statements

> Statements are roughly equivalent to sentences in natural languages. A statement forms a complete unit of execution.

> The following types of expressions can be made into a statement by terminating the expression with a semicolon (;).

> - assignment expressions
> - any use of ++ or --
> - method calls
> - object creation expressions
> In addition to the expression statements described above, declaration statements and control flow statements also constitute a statement. A declaration statement is a statement that declares a local variable. Control flow statements are listed in the following table:

> Statement Type | Keyword
> looping | `while`, `do-while` , `for`
> decision making | `if-else`, `switch-case`
> exception handling | `try-catch-finally`, `throw`
> branching | `break`, `continue`, `label:`, `return`

### Blocks

> A block is a collection of statements between balanced curly braces. The following listing shows two blocks from the program each consisting of a single statement:

> >     if (Character.isUpperCase(aChar)) {
> >         System.out.println("The character " + aChar + " is upper case.");
> >     } else {
> >         System.out.println("The character " + aChar + " is lower case.");
> >     }

> The braces themselves are not considered part of the block; they just delimit it.

| Start of Tutorial > Start of Trail > Start of Lesson | Search