Methods and constructors are defined by sequences of statements, along with variable definitions. The statements specify the sequence of actions to be performed when a method or constructor is invoked. They have many uses such as altering the value of variables, generating output, processing input, or responding to user mouse or keyboard actions.

Java has the following types of statements.

An assignment statement has the following form.

variable = expression;
      

This statement changes the value of the variable on the left side of the equals sign to the value of the expression on the right-hand side. The variable is often just specified by a variable name, but there are also expressions that specify variables.

Java treats an assignment as both an expression and as a statement. As an expression, its value is the value assigned to the variable. This is done to allow multiple assignments in a single statement, such as

a = b = 5;
      

By treating b = 5 as an expression with value 5, Java makes sense of this statement, assigning the value 5 to both a and b.

In Java, any sequence of statements can be grouped together to function as a single statement by enclosing the sequence in braces. These groupings are called statement blocks. A statement block may also include variable declarations.

Statement blocks are used to define methods and to allow multiple statements in the control structures described in the following sections.

Normally, statements in a method or constructor are executed sequentially. Java also has control statements that allow repetitive execution of statements and conditional execution of statements. Java has the following types of control statements.

Java has three kinds of statements that permit execution of a nested statement based on the value of a boolean expression or selection among several statements based on the value of a boolean expression or a control variable. These statements are the if statement, the if-else statement, and the switch statement.

The if statement has the following form.

if (boolean-expression) {
  then-clause
}
      

Here,

The if-else statement has the following form.

if (boolean-expression) {
  then-clause
} else {
  else-clause
}
      

Here,

The switch statement allows execution of different statements depending on the value of an expression. It has the following form.

switch (control-expression) {
case constant-expression-1:
  statements-1
.
.
.
case constant-expression-n:
  statements-n
default:
  default-statements
}
      

Here,

When the switch statement is executed, control-expression is evaluated. The resulting value is compared to the values of constant-expression-1 through constant-expression-n in order until a matching value is found. If a match is found in constant-expression-i then statements-i through statements-n and default-statements are executed, with switch statement execution terminated if a break statement is encountered. Normally, the last statement in each sequence is a break statement so that only one sequence is executed.

The default clause is optional. If it is present then the default-statements are executed whenever the value of control-expression does not match any of the constant-expression-i values.

Often, a programmer needs a construction that works like a switch statement, but the selection between choices is too complex for a switch statement. To make this work, a programmer can use a sequence of if statements where each if statement is nested in the else clause of the preceding if statement. This construction is called an extended if-else statement. It has the following form.

if (boolean-expression-1) {
  statements-1
} else if (boolean-expression-2) {
  statements-2
.
.
.
} else if (boolean-expression-n) {
  statements-n
} else {
  default-statements
}
      

Here,

When this extended if-else statement is executed, the boolean expressions are evaluated in order until one is found that is true. Then the corresponding sequence of statements is executed. If none of the boolean expressions is true then the default-statements are executed. In either case, execution continues with the next statement after the extended if-else statement.

Loop statements allow a nested statement to be executed repetitively. The nested statement can be a block statement, allowing repetition of a sequence of statements.

When a loop is executed its nested statement can be executed any number of times. Each execution of the nested statement is called an iteration of the loop. The number of iterations is controlled by a boolean expression. The boolean expression is evaluated before each iteration (pretest) in while loops and for loops, and after each iteration (post-test) in do-while loops. When the boolean expression becomes false the loop is terminated.

Java has three kinds of loop statements:

The while loop is a pretest loop statement. It has the following form.

while (boolean-expression) {
  nested-statements
}
      

Here,

The boolean expression is tested before each iteration of the loop. The loop terminates when it is false.

The for loop is a pretest loop statement. It has the following form.

for (initialization; boolean-expression; increment) {
  nested-statements
}
      

Here,

When a for loop is executed the initialization expression is evaluated first. This expression is usually an assignment (for example i = 0) that sets the initial value of a loop control variable.

The boolean expression is tested before each iteration of the loop. The loop terminates when it is false. The boolean expression is frequently a comparison (for example, i < 10).

At the end of each iteration, the increment expression is evaluated. The expression is often an expression that increments the control variable (for example, i++).

The do-while loop is a post-test loop statement. It has the following form.

do {
  nested-statements
} while (boolean-expression);
      

Here,

The boolean expression is tested after each iteration of the loop. The loop terminates when it is false.

The return statement is used in the definition of a method to set its returned value and to terminate execution of the method. It has two forms. Methods with returned type void use the following form.

return;
      

Methods with non-void returned type use the following form.

return expression;
      

Here,

The break statement is used in loop (for, while, and do-while) statements and switch statements to terminate execution of the statement. A break statement has the following form.

break;
      

After a break statement is executed, execution proceeds to the statement that follows the enclosing loop or switch statement.

The continue statement is used in while loop, for loop, or do-while loop to terminate an iteration of the loop. A continue statement has the following form.

continue;
      

After a continue statement is executed in a for loop, its increment and boolean expression are evaluated. If the boolean expression is true then the nested statements are executed again.

After a continue statement is executed in a while or do-while loop, its boolean expression is evaluated. If the boolean expression is true then the nested statements are executed again.

Most Java objects are created using the keyword new with a call to a constructor method. A class can provide a default parameterless constructor that is inherited from the Object class or specialized constructors can be defined in the class.

Java constructor definitions are similar to method definitions except for two things:

Most constructors are declared public, so their definition has the form

    public class-name(typed parameter list) {
        object initialization code
    }
      

The typed parameter list has the same syntax as the parameter list in a C function definition.

A constructor is usually called in a new expression, which has the form

    new class-name(parameter list)
      

This expression returns a new instance of the class named by class-name. The expression can be used in any context where an object of this class is legal. For example it could be the right-hand side of an assignment statement to a variable of the appropriate type, or it could be used as a parameter in a method call.

There is one context where a constructor is called without a new expression: inside a constructor for an object of the same class. In this case, the initialization code of the called constructor is executed without creating a new object. Two special syntax forms are used to do this:

    this(parameter list);
      

and

    super(parameter list);
      

The first form performs the initialization code in the constructor whose parameter types match the actual parameters (usually a different constructor). The second form performs the initialization code in a constructor defined in the superclass.

Often, there is one primary constructor for a class, with all of the necessary parameters for constructing an object, and one or more secondary constructors that omit some of the parameters. The initialization code for the secondary constructors is just a call to the primary constructor with default values provided for the omitted parameters. For example, in the String standard library class, there is a primary constructor with a String parameter that returns a copy of the parameter. There is also a secondary constructor that returns an empty String. The code for the secondary constructor is

    public String() {
	  this("");
    }
      

The this statement just calls the primary constructor, providing an empty String as a default value.

When designing the constructors for a class, data integrity is an important consideration. All public constructors should create objects that satisfy data integrity constraints. All classes have a default parameterless constructor that only does initialization specified in the default constructor for the superclass. If that does not create an object that satisfies data integrity constraints then a new default constructor should be defined for the class, overriding the inherited one.

Sometimes there is no reasonable way of defining a parameterless constructor. In that case, the default constructor should be declared as either private or protected. The latter is sometimes useful for allowing subclasses to call the default constructor in their initialization code.

Messages are the fundamental means of communication between objects in a Java program. A message has the following form.

receiver.method-name(parameters)
      

Here,

The receiver can be omitted if it is the object that you are writing code for. That is, you do not need to specify the receiver for messages sent from an object to itself.

Messages can be used in three ways to form statements. First, if the method specified by a message returns a value then the message can be used as the expression in an assignment statement.

variable = message;
      

For messages with methods that do not return values, a statement can also be formed by just terminating the message with a semicolon.

message;
      

This statement form can also be used when the method returns a value, although it is not usually a good idea to ignore a returned value.

Finally, if a message returns an object, then that object can be used directly as the receiver of a message. In an applet method, for example, getContentPane() returns a container to which components can be added. This container is the receiver of the add() message in the following statement.

getContentPane().add(button);
      

The syntax of a Java message closely resembles a C expression that accesses a member of a struct. Its form is one of the following:

    receiver.method-name(parameter list)
      

or

    receiver.variable-name
      

In both forms, receiver is an expression that denotes the receiver of the message. This expression can be a variable or class name, or an indexed array expression, or any complex Java expression that has an object as its returned value. The legality of both forms depends on access conditions, described in the "Java Access Conditions" section.

In the first form, method-name is the name of the method to apply, and parameter list is a possibly empty comma separated list of parameters. Except for the receiver and the period at the front, the first message form has the same syntax as a C function call. Like C function calls, the expression can have effects and can return data. Normally, the direct effects are limited to changes in the receiving object.

In the second form, variable-name is the name of a variable attached to the receiver. The expression returns the value or reference associated with that variable.

For example, consider the following statement, where x is the name of an object variable.

    System.out.println(x.getClass().getName());
      

Here, System is the name of a standard library class. This class has an attached variable, out, which is the standard output stream for a program, much like stdout in C programs. This variable is an object of class PrintStream. The PrintStream class defines the println() method, which prints its argument followed by a newline. The argument should be an object from the String class.

The getClass() method is defined for all objects, returning the class (an object of class Class) to which the object belongs. The Class class defines the getName() method, which returns the name of the lass (an object of class String). Thus the above statement prints the name of the class to which x belongs. With the possible exception of the class of x, all of the classes in this example are Java standard library classes.