Java Expressions and Blocks
Java Expressions and Blocks
Blocks
In this tutorial, you will learn about Java expressions, Java statements,
difference between expression and statement, and Java blocks with the
help of examples.
Java Expressions
A Java expression consists of variables, operators, literals, and method
calls. To know more about method calls, visit Java methods. For example,
int score;
score = 90;
Here, score = 90 is an expression that returns an int . Consider another
example,
if (number1 == number2)
System.out.println("Number 1 is larger than number 2");
Expression statements
// expression
number = 10
// statement
number = 10;
Declaration Statements
The statement above declares a variable tax which is initialized to 9.5 .
Note: 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 Main {
public static void main(String[] args) {
Output:
Hey Jude!
System.out.print("Hey ");
System.out.print("Jude!");
However, a block may not have any statements. Consider the following
examples,
class Main {
public static void main(String[] args) {
} // end of block
}
}
class AssignmentOperator {
public static void main(String[] args) { // start of block
} // end of block
}
Here, we have block public static void main() {...} . However, similar to
the above example, this block does not have any statement.