|The Nuts and Bolts of the Java Language|
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. For example, the statement from the
countCharsmethod is an expression:This particular expression evaluates to the value ofcount++;
countbefore the operation occurs.
The data type of the value returned by an expression depends on the elements used in the expression. The expression
count++returns an integer because
++returns a value of the same data type as its operand and
countis an integer. Other expressions return boolean values, strings, and so on.
countCharsmethod contains, in addition to the
count++expression, a few other expressions, including this one:This expression is interesting because it consists of two expressions. The first expression is a method call:in.read() != -1A method call expression evaluates to the return value of the method; thus the data type of a method call expression is the same as the data type of the return value of that method. Thein.read()
in.readmethod is declared to return an integer so, the expression
in.readevaluates to an integer.
The second expression contained in the statement
in.read() != -1uses the
!=operator. Recall that
!=compares its two operands for inequality. In the statement in question the operands are
in.readis a valid operand for
in.readis an expression and evaluates to an integer. So
in.read() != -1compares two integers, the value returned by
-1. The value returned by
falsedepending on the outcome of the comparison.
As you can see, Java 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. Also, as you may have concluded from the previous example, the order in which a compound expression is evaluated matters!
Take for example this compound expression: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, this expression gives different results depending on whether you perform the addition or the division operation first:x * y * zYou can direct the Java compiler explicitly how you want an expression evaluated with balanced parenthesesx + y / 100
). 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 / 100first. Thusis equivalent tox + y / 100To make your code easier to read and maintain you should be explicit and indicate with parentheses which operators should be evaluated first.x + (y / 100)
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.
 . (params) expr++ expr--
++expr --expr +expr -expr ~ !
creation or cast
* / %
<< >> >>>
< > <= >= instanceof
bitwise exclusive OR
bitwise inclusive OR
= += -= *= /= %= &= ^= |= <<= >>= >>>=
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.
|The Nuts and Bolts of the Java Language|