Java Expressions, Statements and Blocks

January 3, 2019
In this article, you will learn about Java expressions, Java statements, difference between expression and statement, and Java blocks.

We have used expressions, statements and blocks in previous chapters (as Java program is made up of it) without much explaining. Now you know what variables, operators and literals are, it will be easier to understand these concepts.

Java Expressions

Expressions consist of variablesoperatorsliterals and method calls that evaluates to a single value. To learn about method calls, visit Java methods.

Let’s take an example,

int score; 
score = 90;

Here, score = 90 is an expression that returns int.

Double a = 2.2, b = 3.4, result;
result = a + b - 3.4;

Here, a + b - 3.4 is an expression.

if (number1 == number2)
    System.out.println("Number 1 is larger than number 2");

Here, number1 == number2 is an expression that returns Boolean. Similarly, "Number 1 is larger than number 2" is a string expression.

Java Statements

Statements are everything that make up a complete unit of execution. For example,

int score = 9*5;

Here, 9*5 is an expression that returns 45, and int score = 9*5; is a statement.

Expressions are part of statements.

Expression statements

Some expressions can be made into statement by terminating the expression with a ;. These are known as expression statements. For example:

number = 10;

Here, number = 10 is an expression where as number = 10; is a statement that compiler can execute.


Here, ++number is an expression where as ++number; is a statement.

Declaration Statements

Declaration statements declares variables. For example,

Double tax = 9.5;

The statement above declares a variable tax which is initialized to 9.5.

Also, there are control flow statements that are used in decision making and looping in Java. You will learn about control flow statements in later chapters.

Java Blocks

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

class AssignmentOperator {
    public static void main(String[] args) {
    	String band = "Beatles";
    	if (band == "Beatles") { // start of block
    		System.out.print("Hey ");
    	} // end of block

There are two statements System.out.print("Hey "); and System.out.print("Jude!"); inside the mentioned block above.

A block may not have any statements. Consider these examples:

class AssignmentOperator {
    public static void main(String[] args) {
    	 if (10 > 5) { // start of block	
    	 } // end of block
class AssignmentOperator {
    public static void main(String[] args) {  // start of block 	

    } // end of block