Java Statements

When you start learning Java, three things appear in almost every program: statements, semicolons, and curly braces. They may look simple, but understanding how they work is essential because they define how Java code is written, grouped, and executed.

A Java program is made up of instructions that tell the computer what to do. These instructions are generally written as statements. Semicolons mark the end of many statements, while curly braces define blocks of code such as classes, methods, loops, and conditional statements.

Understanding these basic elements will make it much easier to read and write Java programs.

What Is a Java Statement?

A statement is an instruction that tells Java to perform an action. It is one of the fundamental building blocks of a Java program.

For example:

int age = 25;

This is a Java statement. It tells Java to create an integer variable named age and assign the value 25 to it.

Another example is:

System.out.println("Hello, Java!");

This statement tells Java to print Hello, Java! to the console.

Most Java statements end with a semicolon (;).

int number = 10;
System.out.println(number);

Here, there are two separate statements.

Why Are Statements Important?

Statements are important because they represent the individual operations performed by a Java program.

For example:

int a = 10;
int b = 20;
int sum = a + b;

System.out.println(sum);

The program performs several actions:

  • Creates a and assigns 10
  • Creates b and assigns 20
  • Calculates the sum
  • Prints the result

Each instruction forms part of the program’s execution flow.

Java normally executes statements sequentially, from one statement to the next, unless a control-flow structure such as an if statement, loop, method call, exception, or other construct changes that flow.

Types of Java Statements

Java contains several kinds of statements. Some of the most common categories are:

  • Declaration statements
  • Assignment statements
  • Expression statements
  • Conditional statements
  • Loop statements
  • Jump statements
  • Block statements
  • Return statements

Let’s look at the most important ones.

Declaration Statements

A declaration statement creates a variable and specifies its type.

int age;

This declares an integer variable called age.

You can also declare and initialize a variable at the same time:

int age = 25;

Another example:

String name = "Karim";

Here, name is declared as a String and assigned a value.

Assignment Statements

An assignment statement assigns a value to a variable.

int number;

number = 100;

The first statement declares the variable, while the second statement assigns the value 100.

You can also update an existing variable:

number = 200;

The variable now contains 200.

Expression Statements

An expression can also become a statement when it performs an operation such as assignment, increment, decrement, or method invocation.

Examples:

number = 50;
number++;
number--;
System.out.println("Hello");

These are expression statements.

Conditional Statements

Conditional statements allow Java to make decisions.

For example:

int age = 20;

if (age >= 18) {
    System.out.println("You are an adult.");
}

The if statement checks a condition. If the condition is true, Java executes the statement or block inside the curly braces.

Conditional statements include:

if
if-else
else-if
switch

For example:

int marks = 75;

if (marks >= 50) {
    System.out.println("Pass");
} else {
    System.out.println("Fail");
}

Loop Statements

Loops allow Java to execute code repeatedly.

For example:

for (int i = 1; i <= 5; i++) {
    System.out.println(i);
}

The for loop repeatedly executes the statement inside its block.

Java provides several loop structures:

for
while
do-while

Example using while:

int i = 1;

while (i <= 5) {
    System.out.println(i);
    i++;
}

Jump Statements

Jump statements change the normal flow of execution.

Common jump statements include:

break
continue
return

For example:

for (int i = 1; i <= 10; i++) {
    if (i == 5) {
        break;
    }

    System.out.println(i);
}

When i becomes 5, the break statement terminates the loop.

A continue statement skips the current iteration:

for (int i = 1; i <= 5; i++) {
    if (i == 3) {
        continue;
    }

    System.out.println(i);
}

The value 3 is skipped.

Return Statements

The return statement is used to return control from a method and, when applicable, return a value.

static int add(int a, int b) {
    return a + b;
}

The method returns the result of a + b.

For a method that does not return a value, return; can be used to exit the method early:

static void checkAge(int age) {
    if (age < 18) {
        return;
    }

    System.out.println("Eligible");
}

What Is a Semicolon in Java?

A semicolon (;) is used to mark the end of many Java statements.

For example:

int age = 25;

The semicolon tells Java that the statement has ended.

Another example:

System.out.println("Hello");

Without the semicolon, Java will generally report a syntax error.

Why Does Java Use Semicolons?

Java needs a way to determine where one statement ends and another begins.

Consider:

int a = 10;
int b = 20;
int c = 30;

The semicolons clearly separate the statements.

Conceptually, Java can read this as:

Statement 1 → int a = 10;
Statement 2 → int b = 20;
Statement 3 → int c = 30;

This makes the boundaries of statements clear to the compiler.

Where Do You Need a Semicolon?

Semicolons are commonly required after statements such as:

int number = 10;
number = 20;
System.out.println(number);
number++;
return number;

Method calls also normally end with semicolons when they are used as statements:

calculate();

Do if, for, and while Need Semicolons?

This is an important beginner question.

Consider:

if (age >= 18) {
    System.out.println("Adult");
}

There is no semicolon after the closing curly brace because the if statement contains a block.

Similarly:

for (int i = 0; i < 5; i++) {
    System.out.println(i);
}

The for statement itself does not end with a semicolon when it has a block.

The semicolon is used inside the for header to separate its three parts:

for (initialization; condition; update)

For example:

for (int i = 0; i < 5; i++)

There are two semicolons inside the parentheses.

The Empty Statement

Java also allows an empty statement, which consists only of a semicolon:

;

It performs no action.

Although valid Java, an isolated semicolon is rarely useful in normal programs and can sometimes be an accidental mistake.

For example:

if (age >= 18);
{
    System.out.println("Adult");
}

This code does not behave as many beginners expect. The semicolon terminates the if statement, so the block following it is no longer controlled by the if.

This is why accidentally placing a semicolon after a control statement can cause difficult-to-find bugs.

What Are Curly Braces in Java?

Curly braces are:

{ }

They are also called braces or curly brackets.

In Java, curly braces are primarily used to define a block of code.

A block is a group of statements that are treated as a single unit.

For example:

if (age >= 18) {
    System.out.println("Adult");
    System.out.println("You can vote.");
}

Everything between { and } belongs to the if block.

Curly Braces in Classes

A Java class is defined using curly braces.

class Student {

}

The opening brace starts the class body, and the closing brace ends it.

For example:

class Student {
    String name;
    int age;
}

The variables name and age are members of the Student class.

Curly Braces in Methods

Methods also use curly braces to define their bodies.

static void greet() {
    System.out.println("Hello!");
}

The code between { and } belongs to the method.

A method can contain multiple statements:

static void calculate() {
    int a = 10;
    int b = 20;
    int sum = a + b;

    System.out.println(sum);
}

The curly braces define the beginning and end of the method body.

Curly Braces in if Statements

Curly braces are commonly used with conditional statements.

if (age >= 18) {
    System.out.println("Adult");
}

You can place multiple statements inside the block:

if (age >= 18) {
    System.out.println("Adult");
    System.out.println("Eligible for this condition.");
}

Both statements are controlled by the if.

Curly Braces in else

The else block also uses curly braces.

if (age >= 18) {
    System.out.println("Adult");
} else {
    System.out.println("Minor");
}

The first block executes when the condition is true, and the second block executes when the condition is false.

Curly Braces in Loops

Curly braces define the body of a loop.

for (int i = 1; i <= 5; i++) {
    System.out.println(i);
}

The statement inside the braces is executed repeatedly.

You can have multiple statements:

for (int i = 1; i <= 5; i++) {
    System.out.println("Number:");
    System.out.println(i);
}

Both statements belong to the loop body.

What Happens Without Curly Braces?

Java allows certain control statements such as if and loops to control a single statement without braces.

For example:

if (age >= 18)
    System.out.println("Adult");

This is valid Java.

However, when you need multiple statements, braces are necessary:

if (age >= 18) {
    System.out.println("Adult");
    System.out.println("Eligible");
}

For beginners and for maintainable code, using braces consistently is generally clearer.

Consider:

if (age >= 18)
    System.out.println("Adult");
    System.out.println("Eligible");

Only the first println belongs to the if. The second one executes regardless of the condition.

Using braces avoids this ambiguity:

if (age >= 18) {
    System.out.println("Adult");
    System.out.println("Eligible");
}

Nested Curly Braces

Java blocks can exist inside other blocks.

For example:

class Demo {

    static void test() {

        if (true) {

            System.out.println("Hello");

        }
    }
}

Here, there are several levels:

Class
 └── Method
      └── if block
           └── Statement

Each opening { should have a corresponding closing }.

Matching Opening and Closing Braces

One of the most common beginner mistakes is forgetting a closing curly brace.

For example:

class Demo {
    public static void main(String[] args) {
        System.out.println("Hello");
}

There are two opening braces:

class Demo {
public static void main(String[] args) {

But only one closing brace is present.

The corrected version is:

class Demo {
    public static void main(String[] args) {
        System.out.println("Hello");
    }
}

The first } closes the main method, and the second } closes the class.

Statements, Semicolons, and Curly Braces Together

These three concepts work together in almost every Java program.

Consider:

class Calculator {

    public static void main(String[] args) {

        int a = 10;
        int b = 20;

        int sum = a + b;

        System.out.println(sum);
    }
}

Let’s break it down.

The class starts with:

class Calculator {

The opening curly brace begins the class body.

The main method starts with:

public static void main(String[] args) {

Its opening curly brace begins the method body.

Inside the method are statements:

int a = 10;
int b = 20;
int sum = a + b;
System.out.println(sum);

Each statement ends with a semicolon.

Finally:

}

closes the method, and another:

}

closes the class.

A Complete Example

Here is a simple Java program that demonstrates statements, semicolons, and curly braces together:

class Student {

    public static void main(String[] args) {

        String name = "Karim";
        int age = 22;

        if (age >= 18) {
            System.out.println(name + " is an adult.");
            System.out.println("Age: " + age);
        }
    }
}

The program contains:

  • A class block
  • A method block
  • Variable declaration statements
  • An if block
  • Output statements
  • Semicolons at the end of ordinary statements
  • Curly braces defining each block

Understanding the Structure Visually

You can think of the program like this:

class Student
{
    main method
    {
        statement;
        statement;

        if condition
        {
            statement;
            statement;
        }
    }
}

This structure is fundamental to Java programming.

Difference Between a Statement and a Block

A statement is an individual instruction.

Example:

int age = 25;

A block is a group of statements enclosed by curly braces.

Example:

{
    int age = 25;
    System.out.println(age);
}

So, a useful way to remember the difference is:

Statement → individual instruction

Block → group of statements

Difference Between Semicolon and Curly Braces

Semicolons and curly braces have different purposes.

FeatureSemicolon ;Curly Braces { }
Main purposeEnds many statementsDefines a block
Used with variablesYesNo
Used with method callsYesNo
Used to define method bodyNoYes
Used to define class bodyNoYes
Used with if blockUsually inside blockYes
Used with loopsYes in certain partsYes for loop body
Defines a group of statementsNoYes

Common Mistakes With Semicolons

Forgetting a Semicolon

Incorrect:

int age = 25

Correct:

int age = 25;

Forgetting a Semicolon After a Method Call

Incorrect:

System.out.println("Hello")

Correct:

System.out.println("Hello");

Adding an Unwanted Semicolon After if

Incorrect:

if (age >= 18);
{
    System.out.println("Adult");
}

Correct:

if (age >= 18) {
    System.out.println("Adult");
}

Common Mistakes With Curly Braces

Missing a Closing Brace

Incorrect:

class Demo {
    public static void main(String[] args) {
        System.out.println("Hello");
}

Correct:

class Demo {
    public static void main(String[] args) {
        System.out.println("Hello");
    }
}

Extra Closing Brace

Incorrect:

class Demo {
    public static void main(String[] args) {
        System.out.println("Hello");
    }
}
}

There is an extra }.

Putting Statements Outside the Correct Block

For example, a method’s statements must belong to its method body:

class Demo {

    public static void main(String[] args) {
        System.out.println("Hello");
    }
}

Understanding which braces belong together makes these errors much easier to identify.

Indentation and Curly Braces

Indentation does not change Java’s execution rules, but it makes the structure of the program easier for humans to read.

Poorly formatted code:

class Demo {
public static void main(String[] args) {
int age=20;
if(age>=18){
System.out.println("Adult");
}
}
}

The same program with proper indentation:

class Demo {

    public static void main(String[] args) {

        int age = 20;

        if (age >= 18) {
            System.out.println("Adult");
        }
    }
}

The second version is much easier to understand.

Modern IDEs such as IntelliJ IDEA, Eclipse, and VS Code can automatically format Java code.

Java Statements Inside Blocks

A block can contain multiple statements.

{
    int a = 10;
    int b = 20;
    int c = a + b;

    System.out.println(c);
}

The braces determine the boundaries of the block, while semicolons determine the boundaries of the individual statements inside it.

This distinction is important:

{       → block begins

statement;
statement;
statement;

}       → block ends

Scope and Curly Braces

Curly braces also help determine scope.

A variable declared inside a block is generally accessible only within that block and appropriate nested blocks.

For example:

if (true) {
    int number = 100;
    System.out.println(number);
}

The variable number belongs to the if block.

Trying to use it outside that block:

if (true) {
    int number = 100;
}

System.out.println(number);

causes a compilation error because number is out of scope.

This is one reason curly braces are more than just visual symbols: they help define the boundaries within which local variables exist.

Java Code Structure at a Glance

A typical Java program can be viewed as a hierarchy:

Class
│
├── Fields / Variables
│
├── Methods
│   │
│   ├── Statements
│   │
│   └── Blocks
│
└── Other Class Members

For example:

class Demo {

    int number = 10;

    public static void main(String[] args) {

        int x = 20;

        if (x > 10) {
            System.out.println("Greater than 10");
        }
    }
}

The class contains a variable and a method. The method contains statements and an if block.

How the Compiler Treats These Elements

When you compile Java code, the Java compiler checks the syntax and structure of your source code.

For example:

int age = 25;

The compiler recognizes a valid declaration statement.

But:

int age = 25

is incomplete because the semicolon is missing.

Similarly, if braces do not match:

class Demo {
    public static void main(String[] args) {
        System.out.println("Hello");
    }

the compiler detects that the class block has not been properly closed.

Syntax errors involving semicolons and braces are among the first errors beginners encounter when learning Java.

Best Practices

When writing Java programs:

  • Use semicolons correctly at the end of statements.
  • Keep opening and closing braces properly matched.
  • Use consistent indentation.
  • Prefer braces around control-flow bodies, even when a single statement would technically be allowed.
  • Avoid unnecessary semicolons.
  • Format your code regularly using your IDE.
  • Keep blocks logically organized.
  • Use meaningful indentation to make nested structures obvious.

Frequently Asked Questions

What is a statement in Java?

A statement is an instruction that tells Java to perform an operation. Examples include variable declarations, assignments, method calls, and return statements.

Does every Java statement need a semicolon?

Many Java statements require a semicolon, but not every Java construct does. For example, a block-based if statement ends with } rather than a semicolon.

What do curly braces mean in Java?

Curly braces { } define blocks of code. They are used for classes, methods, loops, conditional blocks, and other language constructs.

Can an if statement work without curly braces?

Yes. An if statement can control a single statement without braces:

if (age >= 18)
    System.out.println("Adult");

However, using braces is generally clearer and safer, especially when code may later be expanded.

What happens if I forget a semicolon?

The Java compiler usually reports a syntax error. You must add the missing semicolon before the program can compile successfully.

What happens if I forget a closing curly brace?

The compiler will usually report a syntax error because the structure of the program is incomplete.

Can I put multiple statements inside curly braces?

Yes. A block can contain multiple statements:

{
    int a = 10;
    int b = 20;
    System.out.println(a + b);
}

Are curly braces important for variable scope?

Yes. Blocks help define the scope of local variables. A local variable declared inside a block generally cannot be accessed outside that block.