Learn the key differences between expressions and statements in Java. Understand how expressions produce values, statements perform actions, with examples and beginner-friendly explanations.
Definition: An expression is a combination of variables, operators, and values that produces a single value.
Key point: Expressions always return a value.
Examples:
int a = 5;
int b = 3;
// Arithmetic expression
int sum = a + b; // 'a + b' is an expression that produces 8
// Relational expression
boolean result = a > b; // 'a > b' is an expression that produces true
// Logical expression
boolean check = (a > 2) && (b < 5); // produces true
Inside an expression, you can have:
+ - * / %> < == != >= <=&& || !++ -- !Definition: A statement is a complete line of code that performs an action.
Key point: Statements do not necessarily produce a value, but they execute an action.
Examples:
int a = 5; // declaration statement
int b = 3; // declaration statement
int sum = a + b; // expression inside a statement
System.out.println(sum); // statement (prints value, action performed)
a++; // statement (increments a by 1)
Types of statements in Java:
a = 5;if, for, whileint x = 10;| Feature | Expression | Statement |
|---|---|---|
| Definition | Produces a value | Performs an action |
| Returns value | Yes | No (usually) |
| Example | a + b, x > y, !flag |
int a = 5;, System.out.println(a);, a++; |
| Used in | Assignments, calculations, conditions | Executing code, controlling flow |
Example:
int a = 5 + 3; // '5 + 3' is expression, 'int a = 5 + 3;' is statement
| [Java Expressions: Types, Examples & Practice Questions | Complete Guide](/java/docs/expressions/) |