Java Reference
In-Depth Information
Expression Statement
An expression with a semicolon at the end is called an expression statement. However, not all Java expressions can
be converted to expression statements by appending a semicolon to them. If i and j are two int variables, i + j is
an arithmetic expression. However, i + j; ( i + j with a semicolon) is not a valid expression statement. Only the
following four kinds of expressions can be converted to expression statements by appending a semicolon to them:
Increment and decrement expressions. For example,
num++;
++num;
num--;
--num;
Assignment expressions. For example,
num = 100;
num *= 10;
Object creation expressions. For example,
new String("This is a text");
Note that this statement creates a new object of the String class. However, the new object's
reference is not stored in any reference variable. Therefore, this statement is not very useful. In
some cases, however, you can use such an object creation statement.
Method invocation expressions
You invoke the method println() to print a message on console. When you use the
println() method without semicolon at the end, it is an expression. When you add a
semicolon at the end of the method call, it becomes a statement. For example,
System.out.println("This is a statement");
Control Flow Statement
By default, all statements in a Java program are executed in the order they appear in the program. However, you can
change the order of execution using control flow statements. Sometimes you may want to execute a statement or a set
of statements only if a particular condition is true. Sometimes you may want to execute a set of statements repeatedly
for a number of times or as long as a particular condition is true. All of these are possible in Java using control flow
statements; if and for statements are examples of control flow statements.
A Block Statement
A block statement is a sequence of zero or more statements enclosed in braces. A block statement is generally used
to group together several statements, so they can be used in a situation that requires you to use a single statement.
In some situations, you can use only one statement. If you want to use more than one statement in those situations,
you can create a block statement by placing all your statements inside braces, which would be treated as a single
 
Search WWH ::




Custom Search