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:
You can specify exactly how you want an expression to be evaluated by using balanced parenthesesx * y * z
x + y / 100
(
and )
.
For example to make the previous expression unambiguous, you could write
(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.
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 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."); }