Statements, blocks, and expressions are the three main building elements of the Java language. An expression is a combination of operands and operators that evaluates to a value. Expressions can be used to assign values to variables, perform arithmetic operations, and compare values. A statement is a complete unit of code that performs a specific action, such as declaring a variable or executing a loop. Statements can be grouped together into blocks, which are enclosed in curly braces and treated as a single unit of code. Blocks are commonly used in control structures like if-else statements and loops to group multiple statements together and define their scope. Understanding the concepts of expressions, statements, and blocks is essential for writing effective Java code.
In our previous discussions, we mentioned expressions and statements. But we did not have an elaborate discussion on them. You were not ready as you had to understand variables and operators before learning about expressions, statements, and blocks.
Expressions : Expressions may be built using operators, which compute values. Statements : The core components of statements are the expressions. Block : We may group statements into groups - these groups are called blocks.
Expressions
In simple words, the combination of operators, constants, variables, and method invocation may be regarded as an expression. An expression computes a single value. To produce a value, an expression may be constructed using one or more operands and zero or more operators.
You have already seen expressions. In the following snippets, expressions are illustrated in bold:
int value = 20; System.out.println("Value is: " + value); int result = 11 + 21; // result is now 32 if (value1 == value2) System.out.println("value1 == value2");
An expression gives a value, we refer to this as a returned value. The data type of the return value of the expression depends on the elements used in the expression. The expression value = 20 returns an int because the assignment operator returns a value of the same data type as its left-hand operand; in this case, the value is an int. As you can see from the other expressions, an expression can return other types of values as well, such as a boolean or String.
Expressions can be categorized into the following categories:
Constant Expression
An expression containing only constant values. A constant value doesn’t change.
Example
5
12 + 7 / 6.0
‘v’
Integral Expression
An integer expression produces an integer result after implementing all the automatic and explicit type conversions.
Example
u
u * v
u + int(5.0)
where u and v are integer variables
Relational Expression
A relational expression relates two operands and produces a boolean-type result.
Example
u <= v u + v > 2
Logical/Conditional Expression
A logical/conditional expression combines two or more relational expressions and produces a boolean-type result.
Example
u > v && u == 10 u == 10 || v == 9
Compound Expression
We may also use combinations of the above expressions. Such expressions are known as compound expressions. Java language allows you to construct compound expressions. But there is a catch. The data of the various smaller expressions must match.
Example
1 * 2 *4
This is a multiplication expression. The order in which this expression is evaluated is not important. The result of the multiplication is independent of order: the result is always the same. It does not matter in which order you apply the multiplications. However, this is not true of all expressions.
x + y / 100 // ambiguous If we do the addition operation first and the division operation second then we get a different result from if we do the division operation first and the addition operation second.
Exactly how an expression will be evaluated can be specified using balanced parenthesis: ( and ).
(x + y) / 100 // unambiguous, recommended Here we only have one result as always the addition operation is done first and the division operation second.
What happens if we do not explicitly indicate the operations to be performed? Then the order is determined by the precedence assigned to the operators in use within the expression. First, the operators with higher precedence get evaluated.
Example
x + y / 100 x + (y/100) unambiguous, recommended
These two expressions are equivalent as the division operator has higher precedence than the addition operator.
The practice of writing compound expressions explicitly and indicating with parentheses which operators should be evaluated first makes code easier to read and maintain.
Statements
We can compare statements with sentences in natural languages. They are roughly equivalent. A complete unit of execution is formed by a statement. The following types of expressions can be made into a statement by terminating the expression with a semicolon (;).
- Assignment expressions
- Any use of ++ or —
- Method invocations
- Object creation expressions
Such statements are called expression statements. Here are some examples of expression statements.
// assignment statement valueOne = 23; // increment statement valueOne++; // method invocation statement System.out.println("Hello World!"); // object creation statement Object object = new Object();
There are two other kinds of statements: declaration statements and control flow statements. We declare a variable using a declaration statement. We have already seen many examples of declaration examples.
// declaration statement double valueOne = 23;
The order in which statements get executed is regulated by control flow statements. We will not discuss them here. You’ll learn about control flow statements in a later tutorial.
Blocks
In Java, it is possible to link two or more statements together. This is called a block of code or code block. By surroundings the statements in the block with opening and closing curly braces we can create a block of code. Once we create a block of code or code block, the statements essentially form one logical unit, which we may use anywhere that a single statement may. A block is essentially a group of zero or more statements between curly braces and can be used anywhere a single statement is allowed. For example, the general form of the if using the blocks of code is:
All the statements in the block of codes associated with the if will be executed if the expression evaluates to true. If the expression evaluates to false then the blocks of code associated with else will be executed. Keep in mind that the else is optional and need not be present. Moreover, it is not necessary for both the if and else statements to be blocks of code.
The following BlockDemo program demonstrates the use of blocks:
public class BlockDemo {
public static void main(String[] args) {
boolean someCondition = true;
if (someCondition) { // begin block 1
System.out.println("someCondition is true.");
System.out.println("So the block associated with if will be executed!");
} // end block one
else { // begin block 2
System.out.println("someCondition is false.");
System.out.println("So the block associated with else will be executed!");
} // end block 2
}
}
Output:
someCondition is true.
So the block associated with if will be executed
Remember that a code block represents one individual logical unit. What this means is that under no circumstances could one of the System.out.println() statements in this code fragments execute without the other also executing, may that be associated with the if block or the else block.
One last thing to note - Notice that the statements that appear within the block of codes are intended. Although Java does not care about the indention, it is common practice to indent one level at the start of a block. Indenting makes the code more readable. Also, the placement of the curly braces are arbitrary.
In Java, as you will see, you can use a block of code anywhere you can use a single statement.