Java Reference
In-Depth Information
Because the programming instructions are placed in the method
main
, let us elaborate on
the method
main
a bit more.
The basic parts of the method
main
are the heading and the body. The first line of the
method
main
:
public static void
main(String[] args)
is called the heading of the method
main
.
The statements enclosed between braces (
{
and
}
) form the body of the method
main
.
The body of the method
main
contains two types of statements:
Declaration statements
Executable statements
Declaration statements
are used to declare things such as variables.
Executable statements
perform calculations, manipulate data, create output, accept input,
and so on.
In Java, variables or identifiers can be declared anywhere within a method, but they must
be declared before they can be used.
EXAMPLE 2-24
The following statements are examples of variable declarations:
int
num1;
int
num2;
double
salary;
String name;
EXAMPLE 2-25
Some executable statements that you have encountered so far are the assignment, input,
and output statements.
Suppose that
num1
and
num2
are
int
variables. The following statements are examples of
executable statements:
num1 = 4;
//assignment statement
num2 = console.nextInt();
//input and
//assignment statement
System.out.println(num1 + " " + num2);
//output statement
Search WWH ::
Custom Search