Java Reference
In-Depth Information
The next line in the program is the
single-line comment
, shown here:
This is the second type of comment supported by Java. A single-line comment begins with
a
//
and ends at the end of the line. As a general rule, programmers use multiline comments
for longer remarks and single-line comments for brief, line-by-line descriptions.
The next line of code is shown here:
This line begins the
main( )
method. As mentioned earlier, in Java, a subroutine is called
a
method
. As the comment preceding it suggests, this is the line at which the program will
begin executing. All Java applications begin execution by calling
main( )
. The exact mean-
ing of each part of this line cannot be given now, since it involves a detailed understanding
of several other of Java's features. However, since many of the examples in this topic will
use this line of code, let's take a brief look at each part now.
The
public
keyword is an
access modifier
. An access modifier determines how other
parts of the program can access the members of the class. When a class member is preceded
by
public
, then that member can be accessed by code outside the class in which it is de-
clared. (The opposite of
public
is
private
, which prevents a member from being used by
code defined outside of its class.) In this case,
main( )
must be declared as
public
, since it
must be called by code outside of its class when the program is started. The keyword
static
allows
main( )
to be called before an object of the class has been created. This is neces-
sary because
main( )
is called by the JVM before any objects are made. The keyword
void
simply tells the compiler that
main( )
does not return a value. As you will see, methods
may also return values. If all this seems a bit confusing, don't worry. All of these concepts
will be discussed in detail in subsequent chapters.
As stated,
main( )
is the method called when a Java application begins. Any information
that you need to pass to a method is received by variables specified within the set of par-
entheses that follow the name of the method. These variables are called
parameters
. If no
parameters are required for a given method, you still need to include the empty paren-
theses. In
main( )
there is only one parameter,
String args[ ]
, which declares a parameter
named
args
. This is an array of objects of type
String
. (
Arrays
are collections of similar
objects.) Objects of type
String
store sequences of characters. In this case,
args
receives
any command-line arguments present when the program is executed. This program does
not make use of this information, but other programs shown later in this topic will.