Java Statements
›Short versionOpen if you only have a minute.
- A statement is one instruction. The method finishes it before the next line begins.
- Declarations, assignments, method calls, return, and break end with a semicolon.
- if, while, for, and a brace block do not take a semicolon after them.
- A semicolon written immediately after if (condition) is an empty statement, and that empty statement becomes the body.
- A local name is visible from its declaration until the closing brace of the block that contains it.
Three lines are enough to watch a method work. Read each one as a single thing the program does, then the next.
int price = 40;
int tax = price / 10;
System.out.println(price + tax);The first line reserves the name price and stores 40 in it. The second reads that 40, divides it by 10, and stores 4 under the name tax. The third adds the two stored numbers and writes 44, then a newline. The method walks the lines from top to bottom. Nothing here repeats or skips.
Each line is a . It is one instruction the program carries out and finishes before the next line begins. A line that does something is that small. The rest of this page is about telling those lines apart, and about the braces that hold several of them together.
How many statements are in int a = 5; int b = 10; int sum = a + b; System.out.println(sum);?
Four. Each semicolon closes one instruction. Wrapping those four lines in braces would group them. The block would not add a fifth action.
Where a statement ends
Many statements end with a . It is the period at the end of the instruction. A line that introduces a name, a line that stores a value, and a method call each take one. You can see it on all three lines above.
A group wrapped in braces ends at the closing brace, with no semicolon after that brace. if, while, and for end when the statement they control ends. They do not take a semicolon of their own. The empty statement is the odd case: a semicolon with nothing in front of it, and it does nothing.
An is the part of a line that yields a value. In the second line, price / 10 is the expression. The statement is the whole line, the one that stores the result in tax. The value and the instruction are different jobs. Java will accept an assignment, an increment or decrement, a method call, or a new expression as a statement once a semicolon follows it. A bare calculation such as price / 10; is rejected, because the value would be thrown away.
A name, a store, and a call
A introduces a name. This one says that age exists and that it will hold an int. It does not store a number yet. A later statement has to do that before the method may read age.
int age;An assignment stores into a name that already exists. age = 25 both stores 25 and has 25 as its value. The semicolon turns that expression into a statement, and the value is discarded. What you keep is the store.
age = 25;The form you will write most often folds the store into the declaration. int age = 25; is still one statement. The = 25 is an initializer inside the declaration, not a second instruction running after a bare int age.
int age = 25;Some expressions are allowed to be the whole statement. count++ adds one to count. count-- subtracts one. Written with a semicolon, each is a statement, and the value the expression produces is discarded. What remains is the change to count.
count++;
count--;A method call used the same way jumps into the method and comes back. System.out.println("Hello!") writes the text and a newline. The call is an expression. The semicolon is what makes the line a statement.
System.out.println("Hello!");Creating an object is an expression too. In the line below, new Scanner(System.in) builds a Scanner that reads from standard input, and the declaration stores that reference in scanner. The whole line is one declaration statement. Scanner lives in java.util, so a file that uses it imports that class before the class. A bare new Scanner(System.in); is an expression statement: the constructor runs, then the reference is discarded, and the method cannot read with that scanner.
Scanner scanner = new Scanner(System.in);A semicolon that does nothing
A semicolon alone is an empty statement. It is legal syntax. The program reaches it, does no work, and moves on.
;That empty instruction matters when it is the statement if controls. if (age > 18) chooses whether the statement that follows it runs. The if itself does not end with a semicolon.
if (age > 18);
System.out.println("Adult");The semicolon is the statement the if controls. It does nothing when age is greater than 18, and it does nothing otherwise. println is the next statement inside the surrounding braces, so it runs either way. Indentation is only for the reader. Those braces, or one real statement with no extra semicolon, are what attach work to the if.
Several lines, held together
A groups statements inside braces. To the method, or to an if, the group counts as one statement. Inside the braces the lines still run one after another.
{
int x = 10;
int y = 20;
int sum = x + y;
System.out.println("Sum: " + sum);
}This is the difference worth keeping. score = 100; does one thing and ends. A block holds several lines so they travel together. An if, while, or for that should run more than one instruction needs those braces. With a single statement and no braces, it owns only that next statement. A method that contains code has a block as its body, which is why the statements of main sit inside braces.
Names declared in the block have a that starts at the declaration and ends at the closing brace. x, y, and sum can be used on the later lines inside these braces. After the brace, those names are gone. A later block may declare them again, because the earlier scope has closed.
The same ideas in one method
The method below uses the kinds you just met. score is declared, and a later statement stores 100. name is declared and initialized in one statement. score++ raises the stored score from 100 to 101. The two calls then write the name and that new score.
public class StatementsDemo {
public static void main(String[] args) {
int score;
score = 100;
String name = "Alice";
score++;
System.out.println("Player: " + name);
System.out.println("Score: " + score);
}
}What it prints
Player: Alice Score: 101
Count the instructions in a smaller fragment. Each line that ends with a semicolon is one statement, so there are four. A block wrapped around them would not add a fifth action. It would only group these four so they count as one statement to whatever contains the braces.
int a = 5;
int b = 10;
int sum = a + b;
System.out.println(sum);One statement per line, and an indent for the lines inside a brace, is enough care for a reader. Several statements may share a line, and the program will still run them in order, but the eye loses the count. A missing semicolon, where the language requires one, is a compile error. An extra semicolon after if compiles. It is an empty statement, and the line beneath it is no longer controlled.