In this article, you will learn-
Java Expressions, Statements, and Blocks
In this tutorial, you will find out about Java expressions, Java statements, the contrast between expression and statement, and Java blocks with the help of examples.
In past chapters, we have used expressions, statements, and blocks absent really any clarifying about them. Since you think about factors, operators, and literals, it will be more clear these ideas.
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,
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 a boolean value. Similarly, “Number 1 is larger than number 2” is a string expression.
Java Statements
In Java, every statement is a finished unit of execution. For instance,
int score = 9*5;
Here, we have a statement. The total execution of this statement includes multiplying integers 9 and 5 and afterward allotting the outcome to the variable score.
In the above statement, we have an expression 9 * 5. In Java, expressions are essential for statements.
Expression statements
We can change over an expression into a statement by ending the expression with a ;. These are known as expression statements. For instance,
// expression
number = 10
// statement
number = 10;
In the above example, we have an expression number = 10. Here, by adding a semicolon (;), we have converted the expression into a statement (number = 10;).
Consider another example,
// expression
++number
// statement
++number;
Similarly, ++number is an expression whereas ++number; is a statement.
Declaration Statements
In Java, declaration statements are used for declaring variables. For example,
Double tax = 9.5;
The statement above proclaims a variable tax which is instated to 9.5.
Note: There are control flow statements that are used in decision making and looping in Java. You will find out 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) {
String band = "programming";
if (band == "Programming") { // start of block
System.out.print("Hey ");
System.out.print("Java!");
} // end of block
}
}
Output
Hey Java!
In the above example, we have a block if {….}.
Here, inside the block we have two statements:
System.out.print(“Hey “);
System.out.print(“Java!”);
However, a block may not have any statements. Consider the following examples,
class Main {
public static void main(String[] args) {
if (10 > 5) { // start of block
} // end of block
}
}
This is a valid Java program. Here, we have a block if {…}. However, there is no any statement inside this 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.
Thanks for reading! We hope you found this tutorial helpful and we would love to hear your feedback in the Comments section below. And show us what you’ve learned by sharing your photos and creative projects with us.