1.6 Expressions, Statements, and Blocks in Java


22/05/25

4 mins

Expressions, Statements, and Blocks in Java: The Building Blocks of Every Program

When you're learning Java—or programming in general—understanding how your code is structured is vital. Three core concepts you’ll encounter everywhere are expressions, statements, and blocks. Let’s break these down with simple explanations and hands-on examples pulled from the official Java documentation.

What Is an Expression?

An expression is any piece of Java code made up of variables, operators, and method invocations that, together, evaluate to a single value. You use expressions every time you do calculations, assign values, or compare things in Java.

Examples of expressions:

int cadence = 0; anArray = 100; System.out.println("Element at index 0: " + anArray); int result = 1 + 2; if (value1 == value2) System.out.println("value1 == value2");

Here’s what’s happening:

  • int cadence = 0; — The expression 0 is assigned to cadence.
  • int result = 1 + 2; — The expression 1 + 2 evaluates to 3, which is then stored in result.
  • if (value1 == value2) — The expression value1 == value2 determines if both values are equal and returns a boolean.

The data type of the result matches the types of the variables and operators you use. For example, cadence = 0 returns an int because cadence is an integer variable.

Compound and Parenthesized Expressions

Expressions can also be combined:

1 * 2 * 3

For more complex cases, especially when mixing different operators, use parentheses to make the order of evaluation clear. For example:

(x + y) / 100 // Unambiguous and recommended

Without parentheses, Java will use operator precedence rules, which may not match what you intended:

x + y / 100 // Division happens first, then addition

Always use parentheses to make your intent explicit and your code easier to read.

A Quick Note on Floating Point Arithmetic

Floating point calculations in Java can sometimes give surprising results due to how numbers are stored:

double d1 = 0.1 + 0.1 + ... + 0.1; // ten times System.out.println("d1 == 1 ? " + (d1 == 1.0));

You might expect true, but you’ll actually get false—it’s a classic example of floating point precision issues in computing.

What Is a Statement?

A statement is like a sentence in natural language—it’s a complete instruction for the computer. In Java, statements end with a semicolon (;). If an expression is finished with a semicolon, it becomes an expression statement.

Common statement types:

  • Assignment statement:
    aValue = 8933.234;
  • Increment statement:
    aValue++;
  • Method invocation:
    System.out.println("Hello World!");
  • Object creation:
    Bicycle myBike = new Bicycle();
  • Declaration statement:
    double aValue = 8933.234;
  • Control flow statements: Change how instructions execute (like if, for, or while). These are covered elsewhere in the Java documentation.

What Is a Block?

A block is a group of zero or more statements wrapped by curly braces {}. You can use a block anywhere you would use a single statement—most often with control flow instructions.

Example usage in an if-else block:

class BlockDemo { public static void main(String[] args) { boolean condition = true; if (condition) { // begin block 1 System.out.println("Condition is true."); } // end block one else { // begin block 2 System.out.println("Condition is false."); } // end block 2 } }

Blocks help organize code, define the scope of variables, and make your programs easier to understand.

In Summary

  • Expressions do the work and return a value.
  • Statements are complete instructions that do something.
  • Blocks group statements together and control scope.

Mastering these three concepts is the foundation for writing clean and correct Java code. They're the essential building blocks that all your future Java projects will use!

Happy coding!


PREVIOUS: 1.5 Mastering Operators in Java

NEXT: 1.7 Switch Statement Java