Scrivio Interview library
ConceptIntermediate6 min read · updated 12 November 2026

Explain the difference between statements and expressions in Java, including expression statements, the empty statement, and why switch can be both. Give a bug caused by an empty statement.

An expression produces a value, such as a + b or list.size(), while a statement performs an action, such as an if, a loop or a return. An expression statement is an expression used as a statement, and Java allows only those that do something: assignments, increments and decrements, method calls and object creation. The empty statement is a lone semicolon, and it causes the classic bug if (x > 0); { doWork(); }, where the semicolon is the if body and doWork() always runs. switch can be both because since Java 14 it has a statement form and an expression form that yields a value.

Why this question is asked

The difference between statements and expressions sounds like grammar school, but it explains a lot of Java's behaviour: why x + 1; does not compile, why a stray semicolon can make code run that should not, why we needed yield, and why some constructs can be used inside other code while others cannot. Interviewers ask it at this level to see whether we can reason about the language's rules instead of memorising them, and the empty statement bug is a real one that reviews still miss. In this answer, we will recap the two ideas, look at which expressions may stand alone, reproduce the empty statement bug, see how switch became both, and finish with blocks, declarations and the tools that catch these mistakes.

1A Quick Recap: Values and Actions

An expression is a piece of code that evaluates to a value. 2 + 3 is an expression with the value 5, name.length() is an expression with the length of the string, and x > 0 is an expression with a boolean value. Expressions can be nested inside other expressions, which is how we build bigger calculations.

A statement is a piece of code that does something and has no value of its own. if, while, for, return, throw and a block in braces are all statements. A method body is a sequence of statements, and the program runs them one after another. So the rule of thumb is: expressions compute, statements act, and statements contain expressions, for example the condition of an if.

Statements and expressions in Java
CodeKindNotes
a + bexpressionhas a value, cannot stand alone
x = 5;expression statementan assignment used as a statement
i++;expression statementincrement
list.add(x);expression statementmethod call, the result is discarded
new Thread(task);expression statementobject creation
if (c) { } / while (c) { }statementno value
int n = 3;declaration statementa statement, never an expression
;empty statementdoes nothing
switch (d) { ... }statement or expressionan expression since Java 14

2Which Expressions May Stand Alone

An expression statement is an expression followed by a semicolon and used as a statement. Java does not allow every expression here, only those that have an effect: assignments, prefix and postfix increments and decrements, method invocations and class instance creation. The reason is to catch mistakes. A line like x + 1; computes a value and throws it away, which is almost certainly a bug, so the compiler rejects it as not a statement.

Java
int x = 5;
x + 1; // error: not a statement
x == 5; // error: not a statement (probably meant x = 5)
x = x + 1; // fine: assignment, x is 6
x++; // fine: increment, x is 7
Math.max(x, 10); // fine: a method call, even though the result is ignored
new StringBuilder("a"); // fine: object creation

Notice that a method call is allowed even when its result is ignored, because the compiler cannot know whether the method has side effects. That is why discarding the result of s.toUpperCase() compiles and silently does nothing useful, and why static analysis tools watch for ignored return values.

3The Empty Statement Bug

A lone semicolon is a statement too: the empty statement, which does nothing. It is legal wherever a statement is allowed, including as the body of an if or a loop. That is the source of the classic bug: a semicolon right after the condition becomes the whole body of the if, and the block in braces that follows is just an ordinary block that always runs.

Java
int balance = 100;
if (balance < 0); { // the ; is the body of the if
sendAlert(); // runs every time, even with a positive balance
}
for (int i = 0; i < 3; i++); { // the loop body is empty
System.out.println("tick"); // prints once, not three times
}
Step throughHow the compiler reads if (balance < 0); { sendAlert(); }
  1. Parse if (balance < 0)
  2. Take the next statement as the bodythe lone ; is the empty statement
  3. The if is complete
  4. Parse the block { sendAlert(); }an ordinary statement after the if
  5. Run time: the block always runswhatever the balance is

5 steps in order. Play them, or step through them one at a time.

The braces look like the body of the if, but the semicolon got there first.

Notice that the code is perfectly legal and is indented as if it were right. The compiler reads if (balance < 0) followed by the empty statement, and then a block that is the next statement in the method. The same mistake on a while loop, while (queue.isEmpty());, creates a busy loop that spins forever.

4Why switch Can Be Both

Before Java 14, switch was only a statement, so computing a value meant declaring a variable and assigning it in every case, with a break after each. Java 14 added the switch expression, which produces a value and can be used anywhere an expression can, such as the right side of an assignment or an argument to a method. To keep old code working, the statement form still exists, and the compiler decides which one we wrote from where the switch appears.

Java
// switch statement: performs actions
switch (day) {
case 6, 7 -> System.out.println("weekend");
default -> System.out.println("weekday");
}
// switch expression: produces a value
int hours = switch (day) {
case 6, 7 -> 0;
default -> {
int base = 8;
yield base; // a block arm gives its value with yield
}
};

Notice the yield. A block arm contains statements, but the switch expression needs a value, so Java added yield to say which value the block produces. return could not be reused, because return already means leaving the whole method.

5Blocks, Scope and Declarations

A block, statements in braces, is itself a statement, and it introduces a scope: a variable declared inside it exists only until the closing brace. That is why the same name can be declared in two separate blocks. A local variable declaration is also a statement, but it is not an expression, so it cannot be used where a value is expected, and Java even forbids it as the direct body of an if without braces, because a variable that goes out of scope immediately would be useless.

Java
{ int temp = 1; }
{ int temp = 2; } // fine: each temp lives in its own block
if (ready) int n = 3; // error: variable declaration not allowed here
if (ready) { int n = 3; } // compiles, but n is gone after the brace

The tools help here. javac -Xlint:empty warns about an empty statement after an if, IDEs flag suspicious semicolons, and a style rule that every if and loop body uses braces, enforced by Checkstyle or a formatter, makes the empty statement bug almost impossible to write.

6Follow-up questions

Is an assignment an expression or a statement?

An expression, with the assigned value as its value, which is why a = b = 0 works and why while ((line = reader.readLine()) != null) is a common idiom. Followed by a semicolon, it becomes an expression statement.

Is a lambda body a statement or an expression?

It can be either. x -> x * 2 has an expression body whose value is returned, while x -> { log(x); return x * 2; } has a block body of statements. A lambda with an expression body can implement a method returning void only if the expression is one that could be an expression statement, such as a method call.

Can if be an expression in Java?

No. Java's if is only a statement. When we need a value from a condition, we use the conditional operator c ? a : b, or a switch expression for more than two cases.

7Conclusion

In this answer, we saw that expressions produce values and statements perform actions, and that only assignments, increments and decrements, method calls and object creation may stand alone as expression statements. A lone semicolon is the empty statement, and after an if or a loop header it silently becomes the body, so the block after it always runs. switch is both a statement and, since Java 14, an expression that yields a value. Blocks create scope, declarations are statements but not expressions, and javac warnings, IDEs and a brace rule catch the empty statement bug.

What to say in the interviewAbout 90 seconds aloud

Open with

An expression evaluates to a value, like a + b, x > 0 or name.length(), and expressions can nest inside each other. A statement performs an action and has no value, like if, while, return or a block.

Then, if they want more

An expression statement is an expression used as a statement with a semicolon, and Java only allows the kinds that do something: assignments, increments and decrements, method calls and object creation. So x + 1; is rejected as not a statement, which catches mistakes.

The empty statement is a lone semicolon, and it is legal anywhere a statement is, including as the body of an if or a loop. The classic bug is if (x > 0); followed by a block: the semicolon is the body, the if ends there, and the block is just the next statement, so doWork always runs.

The same with a for loop makes the body run once. switch can be both because Java 14 added the switch expression, which produces a value and uses yield in block arms, while the old statement form still works. I would also mention that a block creates scope and that declarations are statements but not expressions.

In practice, a rule that every if and loop uses braces, plus javac's -Xlint:empty and IDE warnings, prevents the empty statement bug.

About this answer

Last updated
12 November 2026
Versions it names
Java 14
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "Explain the difference between statements and expressions in Java, including expression...": https://scrivio-interview-library.netlify.app/java/operators-and-control-flow/fecc204a3c/