EMZETT.
Login

Statements

In short: A single instruction terminated with a semicolon — the smallest executable building block in Java (e.g. an assignment, a method call, a declaration).

In more detail: Several statements together form a block ({ ... }), e.g. the body of a method or loop. Control structures like if or for aren’t themselves statements in the narrow sense, but control WHICH statements are executed.

In Depth

int number = 5;                    // declaration statement
number = number + 1;                 // expression statement (assignment)
System.out.println(number);        // expression statement (method call)

Java roughly distinguishes between expression statements (something happens and terminates with ;, e.g. an assignment, a method call, i++) and control-flow statements, which themselves need NO semicolon at the end, because they enclose a block (if, for, while, switch). An empty statement (just a semicolon ;) is syntactically valid, but usually a typo — e.g. for (int i = 0; i < 10; i++); with an accidental semicolon right after the loop condition results in the actually intended loop body running only ONCE after the loop ends, instead of ten times.

Several statements within curly braces { ... } form a block, which is syntactically treated as ONE single statement — which is why, anywhere Java expects a statement (e.g. after an if with no braces), you can insert either exactly one single statement OR an entire block.

Statement vs. expression

A subtle but important difference: an EXPRESSION always delivers a value (5 + 3, number > 0, a method call with a return value), a STATEMENT, by contrast, is a complete instruction that doesn’t itself deliver a value, but causes something to happen. An expression becomes a statement when a semicolon is appended (number + 3; is syntactically valid but pointless, since the result goes nowhere) — but not every expression is allowed to stand as a standalone statement: 5 + 3; alone is syntactically an “expression statement”, but the Java compiler only allows certain expression types as standalone statements (assignments, increment/decrement, method calls, object creation) — a pure arithmetic expression like 5 + 3; with no assignment is therefore a compiler error.

Declaration, expression, and control-flow statements

Java statements can roughly be divided into three categories:

// Declaration statement: creates a new variable
int number;
 
// Expression statement: an assignment, a call, an increment
number = 5;
number++;
System.out.println(number);
 
// Control-flow statement: controls WHICH other statements run
if (number > 0) {
    System.out.println("positive");
}

Control-flow statements like if, for, while, and switch themselves contain other statements again (often as a block in { }) — Java programs are ultimately nested tree structures of such statements, which the compiler translates into exactly this tree form (the “abstract syntax tree”) while parsing, before bytecode is generated from it.

See also: Print Variables, if Statement