Statements.
A Java program is composed of statements , which define the computation by creating and manipulating variables, assigning data-type values to them, and controlling the flow of execution of such operations.- Declarations create variables of a specified type and name them with identifiers. Java is a strongly typed language because the Java compiler checks for consistency. The scope of a variable is the part of the program where it is defined.
- Assignments associate a data-type value (defined by an expression) with a variable.
- Initializing declarations combine a declaration with an assignment to initialize a variable at the same time it is declared.
- Implicit assignments. The following shortcuts are available when our purpose is to modify a variable's value relative to the current value:
- Increment/decrement operators: the code i++ is shorthand for i = i + 1. The code ++i is the same except that the expression value is taken after the increment/decrement, not before.
- Other compound operators: the code i /= 2 is shorthand for i = i/2.
- Conditionals provide for a simple change in the flow of execution—execute the statements in one of two blocks, depending on a specified condition.
- Loops provide for a more profound change in the flow of execution—execute the statements in a block as long as a given condition is true. We refer to the statements in the block in a loop as the body of the loop.
- Break and continue. Java supports two additional statements for use within while loops:
- The break statement, which immediately exits the loop
- The continue statement, which immediately begins the next iteration of the loop
- For notation. Many loops follow this scheme: initialize an index variable to some value and then use a while loop to test a loop continuation condition involving the index variable, where the last statement in the while loop increments the index variable. You can express such loops compactly with Java's for notation.
- Single-statement blocks. If a block of statements in a conditional or a loop has only a single statement, the curly braces may be omitted.
Input and output.
- Commands and arguments.
- Standard output.
- Formatted output.
- Standard input.
- Redirection and piping.
- Input and output from a file.
- Standard drawing.