skip to content

In Java, what is the difference between an expression and a statement?

level: juniorimportance: must knowfreq 70%

answer

  1. Expression = has a value; statement = does an action
  2. Expression + semicolon = expression statement
  3. Every expression -> statement, but not the reverse
  4. if/for/while have no value; ternary and switch-expression do

basics

~20 s

An expression produces a value (like 2 + 3 or x). A statement performs an action and is a complete instruction the program runs (like x = 5; or an if). Expressions compute things; statements do things.

solid answer

~50 s

An expression is a piece of code that evaluates to a single value: a literal (42), a variable (x), a method call that returns something (list.size()), or a combination using operators (a + b). A statement is a complete unit of execution that the program performs, terminated by a semicolon or grouped in braces: assignments, control flow (if, for, while), declarations, and method invocations used for their effect. The relationship is asymmetric: any expression can be turned into a statement by adding a semicolon (an 'expression statement'), but not every statement is an expression. For example, an if block or a while loop has no value, so you cannot write int x = (if (a) 1 else 2); in Java the way you would in some other languages. Expressions are the building blocks evaluated inside statements; statements sequence the program's actions.

code

java · 7 lines
java
int a = 2 + 3;      // 2 + 3 is an EXPRESSION; the whole line is a declaration STATEMENT
int b;              // declaration statement (no value)
b = a * 2;          // a * 2 is an expression; b = a * 2 is an assignment expression; the line is an expression statement
System.out.println(b); // method call expression used as a statement
// a + b;           // ERROR: 'not a statement' — a bare value-producing expression isn't allowed
int max = (a > b) ? a : b; // ternary is an EXPRESSION, so it can appear on the right of =
;                   // the empty statement: legal, does nothing

go deeper

for a junior

Can state the core rule: expressions produce a value, statements perform an action, and give one example of each.

for a middle

Explains the asymmetry (every expression can become a statement, not vice versa), names expression statements, and knows the ternary is the expression alternative to an if statement.

for a senior

Articulates the categories of statements, why only some expressions are legal as statements, and connects the distinction to switch expressions vs statements and lambda body forms.

for a principal

Frames the distinction in terms of language design — how expression-orientation (everything yields a value) vs statement-orientation shapes a language, and the trade-offs Java made and is gradually relaxing (switch/expression-bodied features).

## First principles When a program runs, it does two kinds of things: it **computes values**, and it **performs actions**. Java gives these two activities two distinct syntactic categories. ### Expression — something that has a value An **expression** is any fragment of code that, when evaluated, **produces (yields) a single value**. "Evaluating" just means the program works out what the value is. Examples: - A **literal**: `42`, `"hello"`, `true` — already a value. - A **variable reference**: `x` — its value is whatever is currently stored in `x`. - An **operator combination**: `a + b`, `count * 2`, `x > 0` (this last one yields a `boolean`). - A **method call that returns a value**: `Math.max(a, b)`, `list.size()`. - An **assignment**: surprisingly, `x = 5` is itself an expression in Java whose value is the value assigned (`5`); that is why `a = b = 0` works (chained assignment). Every expression has a **type** (the kind of value it produces, e.g. `int`, `String`, `boolean`). ### Statement — a complete instruction that does something A **statement** is a **complete unit of execution** — one step of the program. It is performed for its **effect**, not necessarily for a value. Java has several kinds of statements: - **Expression statement**: an expression followed by a semicolon, e.g. `x = 5;`, `list.add(item);`, `i++;`. Only certain expressions are legal here (assignments, increments/decrements, method calls, object creation) — you cannot write `a + b;` as a statement because computing a sum and throwing it away is pointless, so the compiler rejects it. - **Declaration statement**: introduces a variable, e.g. `int count = 0;`. - **Control-flow statements**: `if`, `for`, `while`, `switch`, `return`, `break`, `continue`, `throw`. - **Block**: `{ ... }` — a group of statements treated as one. - **The empty statement**: a lone semicolon `;` that does nothing (a valid statement on its own). ### The key asymmetry **Every expression can become a statement** by adding a semicolon (when the expression kind is allowed). **But not every statement is an expression** — an `if` block, a `for` loop, or a variable declaration has **no value**, so you cannot use it where a value is expected: ```java int y = if (cond) 1 else 2; // NOT valid Java ``` In Java you reach for the **ternary conditional operator** `cond ? 1 : 2`, which *is* an expression, or (since Java 14) a **switch expression** `switch (...) { ... }` that yields a value. Plain `if`/`switch` *statements* still produce nothing. ### Why it matters Understanding the divide explains many compiler errors ("not a statement", "if is not allowed here"), tells you where you may put code (only expressions go inside `( )` of an `if`, only statements go in a method body line by line), and clarifies modern features like switch **expressions** vs switch **statements**, lambda bodies (an expression-bodied lambda `x -> x + 1` vs a block-bodied one), and why `i++;` is legal but `i + 1;` is not.

  • Is x = 5 an expression or a statement in Java?
    Both, depending on context. x = 5 is an assignment expression whose value is 5; add a semicolon and the whole line x = 5; is an expression statement. That dual nature is why chained assignment a = b = 5 works.
  • Why does the compiler reject a + b; as a line of code?
    Because it is an expression statement, but only assignment, increment/decrement, method-call, and object-creation expressions are allowed as statements. a + b just computes a value and discards it, which is presumed to be a mistake, so the compiler reports 'not a statement'.

An expression is a noun phrase that names a value ('the sum of a and b'); a statement is a full sentence that gives a command ('store that sum in x.'). You can build sentences out of noun phrases, but a noun phrase alone is not a command.

saying these in an interview costs you the question

  • Saying every statement is also an expression — if/for blocks have no value
  • Claiming you can write int y = if(cond) 1 else 2; in Java (you cannot)
  • Thinking expressions and statements are the same thing with different names
  • Believing any expression can be a statement — only certain kinds (assignment, call, ++/--, new) can

context