Understanding Java Expressions, Statements, and Blocks
Understanding Java Expressions, Statements, and Blocks
This guide explains Java expressions, statements, and blocks with practical examples, drawing on the Java Language Specification (JLS) for authoritative clarity.
In earlier chapters we used expressions, statements, and blocks without delving into their mechanics. Now that you’re comfortable with variables, operators, and literals, let’s explore these core concepts in depth.
Java Expressions
A Java expression combines variables, operators, literals, and method calls to produce a value. For example:
int score;
score = 90;
Here, score = 90 is an expression that evaluates to an int. Another illustration:
Double a = 2.2, b = 3.4, result;
result = a + b - 3.4;
In this snippet, a + b - 3.4 is an arithmetic expression yielding a Double. Expressions can also produce boolean values, such as in conditionals:
if (number1 == number2)
System.out.println("Number 1 is larger than number 2");
Here, number1 == number2 returns a boolean, and the string inside println is a string literal expression.
Java Statements
A statement is a complete unit of execution. For example:
int score = 9*5;
This statement multiplies 9 by 5 and assigns the result to score. Notice that the expression 9 * 5 is embedded within the statement.
Expression Statements
Any expression can be turned into a statement by terminating it with a semicolon (;). These are called expression statements. For instance:
// expression
number = 10
// statement
number = 10;
Here, number = 10 is an expression; adding the semicolon turns it into a statement. The same rule applies to increment operators:
// expression
++number
// statement
++number;
Both forms are valid, but only the latter executes during runtime.
Declaration Statements
Java uses declaration statements to create variables. Example:
Double tax = 9.5;
This statement declares a variable tax and initializes it to 9.5. Declaration statements are essential for type safety and are defined in the JLS §4.2.
Tip: Control flow statements—such as if, for, and while—are covered later. They govern decision making and looping.
Java Blocks
A block groups zero or more statements within curly braces ({ }). Example:
class Main {
public static void main(String[] args) {
String band = "Beatles";
if (band == "Beatles") { // start of block
System.out.print("Hey ");
System.out.print("Jude!");
} // end of block
}
}
Output:
Hey Jude!
The block if { … } contains two statements. A block may also be empty, as shown below:
class Main {
public static void main(String[] args) {
if (10 > 5) { // start of block
} // end of block
}
}
Even without statements, the block is syntactically valid. Similarly, a method body can be an empty block:
class AssignmentOperator {
public static void main(String[] args) { // start of block
} // end of block
}
Blocks are defined in the JLS §14 and are fundamental for scoping and control flow.
Java
- C# Expressions, Statements & Blocks: A Comprehensive Guide with Practical Examples
- Understanding Java: JDK, JRE, and JVM Explained
- Java Variables and Literals: A Comprehensive Guide
- Mastering Java While & Do‑While Loops: Step‑by‑Step Tutorial
- Java Classes and Objects: A Practical Guide
- Java Abstract Classes and Methods: A Comprehensive Guide
- Mastering Java Nested and Inner Classes: Types, Examples, and Best Practices
- Mastering Java Lambda Expressions: From Basics to Stream API
- Java Regular Expressions: Mastering Pattern Matching with java.util.regex
- Mastering Java 8 Lambda Expressions: A Guide to Functional Programming