English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Java Basic Tutorial

Java Flow Control

Java Array

Java Object-Oriented (I)

Java Object-Oriented (II)

Java Object-Oriented (III)

Java Exception Handling

Java List

Java Queue (Queue)

Java Map Collection

Java Set Collection

Java Input/Output (I/O)

Java Reader/Writer

Java Other Topics

Java expressions, statements, and code blocks

In this tutorial, you will learn about Java expressions, Java statements, the difference between expressions and statements, and Java blocks through examples.

In the previous chapters, we used expressions, statements, and blocks without explaining them in detail. Since you have already learned about variables, operators, and literals, it will be easier to understand these concepts.

Java expressions

Java expressions are composed ofVariablesOperators,Operators,LiteralsJava Methodsand method calls. To learn more about method calls, please visit

. For example, 
int score; 9score =

0; 9score =

0 is an expression that returns an int data type. Let's look at another example, 2.2Double a = 3.4, b =
, result; + Here, a - 3.4;

result = a + Here, a - 3.4b

is an expression.1 if (number2== number
    )1is a comparison expression. Similarly,"2System.out.println("

large");1 Here number2== number1is a comparison expression. Similarly,"2"large" is a string expression.

Java statement

In Java, each statement is a complete execution unit. For example,

int score = 9*5;

Here, we have a declaration. The complete execution of this statement involves9and5Multiplication of integers, then assigning the result to the variable score.

In the above statement, we have an expression9 * 5. In Java, an expression is part of a statement.

Expression statement

We can convert an expression into a statement by using a terminating expression; These are called expression statements. For example,

// expression
number = 10
// statement
number = 10;

In the above example, we have an expression number = 10. By adding a semicolon (;), we have converted the expression into a statement (number = 10);.

Let's look at another example,

// expression
++number
// statement
++number;

Similarly,++number; is an expression, whereas++; is a statement.

Declaration statement

In Java, declaration statements are used to declare variables. For example,

Double tax = 9.5;

The above statement declares a variable tax and initializes it with the value9.5.

Note:Control flow statements are used in Java for decision-making and loops. You will learn about control flow statements in the following chapters.

Java code block

A block is a group of statements (zero or more) enclosed in curly braces { }. For example,

class Main {
    public static void main(String[] args) {
    	
        String band = "Beatles";
    	
        if (band == "Beatles") { // Start of Code Block
            System.out.print("Hey ");
            System.out.print("Jude!");
        } //End of Code Block
    }
}

Output:

Hey Jude!

In the above example, we have an if {....} code block.

Here, inside the block, we have two statements:

  • System.out.print("Hey ");

  • System.out.print("Jude!");

However, a block can be empty. For example, the following example,

class Main {
    public static void main(String[] args) {
    	
        if (10 > 5) { // Start of Code Block
 
        } // End of Code Block
    }
}

This is a valid Java program. Here, we have an if {...} code block. However, there are no statements inside this block.

class AssignmentOperator {
    public static void main(String[] args) {  // Start of Code Block
    } //End of Code Block
}

Here, we have the code block public static void main() {...}. However, like the above example, this block contains no statements.