We actually defined a statement once in Chapter 2, Java Language Basics. It is a complete action that can be executed. It can include one or more expressions and ends with a semicolon ;.
A Java statement describes an action. It is a minimal construct that can be executed. It may or may not include one or more expressions.
The possible kinds of Java statements are:
- A class or interface declaration statement, such as class A {...}
- An empty statement that consists of only one symbol, ;
- A local variable declaration statement, int x;
- A synchronized statement – outside the scope of this book
- An expression statement, which can be one of the following:
- A method invocation statement, such as method();
- An assignment statement, such as x = 3;
- An object creation statement, such as new SomeClass();
- A unary increment or decrement statement, such as ++x ; --x; x++; x--;
- A control flow statement (see Chapter 10, Control Flow Statements):
- A selection statement: if-else or switch-case
- An iteration statement: for, while, or do-while
- An exception handling statement, such as try-catch-finally or throw
- A branching statement, such as break, continue, label:, return, assert
A statement can be labeled by placing an identifier and colon : in front of it. This label can be used by the branching statements break and continue to redirect the control flow. In Chapter 10, Control Flow Statements, we will show you how to do it.
Most often, statements compose a method body, and that is how programs are written.