Start of Tutorial > Start of Trail > Start of Lesson | Search |
[PENDING: this could use some tightening of the language and cleanup]
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:Each of these expressions perform an operation and return a value.... // 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)) { ... }
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 oflargestByte
converted to a stringThe 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 andaChar
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:
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 * zYou can direct the Java compiler explicitly how you want an expression evaluated with balanced parenthesesx + y / 100(
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 evaluatey / 100
first. 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.
#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 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 (;).
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:
- assignment expressions
- any use of ++ or --
- method calls
- object creation expressions
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
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:The braces themselves are not considered part of the block; they just delimit it.if (Character.isUpperCase(aChar)) { System.out.println("The character " + aChar + " is upper case."); } else { System.out.println("The character " + aChar + " is lower case."); }
Start of Tutorial > Start of Trail > Start of Lesson | Search |