A compound statement is a statement that is based on the entire chunk, so it is called a block statement.
The compound statement is represented by the curly brace "{", "}".
In fact, we have contacted the compound statement, such as when defining a class or method, the class body is the "{}" symbol as the beginning and end of the tag, the method body is the same.
Each statement in a compound statement is executed from top to bottom, the compound statement is in the entire block, can be used anywhere a single statement can be used, and compound statements can also be nested compound statements.
The Compound statement instance (////* These two symbols represent comments):
Public class test{//Create Class
Public static void Main (string[] args) {//Main method
int a = 1; declares an int variable and assigns a value of 1
//b,c cannot be used in this scope.
{
//b,c can be used in this scope
int b = 2; declares an int variable and assigns a value of 1
int c = 3; declares an int variable and assigns a value of 1
Boolean D; Declaring a Boolean variable
{
d = b > C; The Boolean variable D is false at this time
}
}
}
}
PS: When using compound statements, be aware that a compound statement creates a scope for a local variable that is part of a program that is created and can be used in that scope. If the variable is used outside the scope of a variable, an error occurs. Example: The B,C variable in this example.
<java basic > Compound statement <6>