Java Expressions, Statements and Blocks
In the previous Post, we have used expressions, statements and blocks without explaining them too much. Now that you know variables, operators, and literals, it will be easier to understand these concepts.
Java expressions
A Java expression consists of variables, operators, literals, and method calls. To learn more about method calls, visit Java Methods. For example,
int score;
score = 90;
Here, score = 90 is an expression that returns an int. Take 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. Likewise, "Number 1 is larger than number 2" is a string expression.
Java Statements
In Java, each instruction is a complete unit of execution. For example,
int score = 9*5;
Here we have a statement. The complete execution of this instruction consists in multiplying the integers 9 and 5, then assigning the result to the score variable.
In the above instruction, we have an expression 9 * 5. In Java, expressions are part of the instructions.
Expression statements
We can convert an expression to an instruction by ending the expression with a ;. These are called expression statements. For example,
// expression
number = 10
// statement
number = 10;
In the example above, we have an expression number = 10. Here, by adding a semicolon (;), we have converted the expression to an instruction (number = 10;).
Take another example,
// expression
++number
// statement
++number;
Likewise, ++number is an expression while ++number; is a statement.
Declaration statements
In Java, declaration statements are used to declare variables. For example,
Double tax = 9.5;
The above statement declares a variable tax which is initialized to 9.5.
Note: There are control flow statements used in decision making and looping in Java. You will learn more about control flow statements in the following chapters.
Java blocks
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 block
System.out.print("Hey ");
System.out.print("Jude!");
} // end of block
}
}
Output:
Hey Jude!
In the example above, we have an if {....} block.
Here inside the block we have two declarations:
- System.out.print ("Hey");
- System.out.print ("Jude!");
However, a block can contain no instructions. 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 are no instructions 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, as in the example above, this block has no instructions.
Comments
Post a Comment