Java Reference
In-Depth Information
main Is a void Method
The main part of a program is a void method, as indicated by its heading:
public static void main(String[] args)
The word static will be explained in Chapter 5. The identifier args is a parameter of type
String[] , which is the type for an array of strings. Arrays are discussed in Chapter 6, and
you need not be concerned about them until then. In the examples in this topic, we never
use the parameter args . Because args is a parameter, you may replace it with any other
nonkeyword identifier and your program will have the same meaning. Aside from possibly
changing the name of the parameter args , the heading of the main method must be
exactly as shown above. Although we will not be using the parameter args , we will tell you
how to use it in Chapter 6.
A program in Java is just a class that has a main method. When you give a command to run
a Java program, the run-time system invokes the method main .
It is important to note that only the value of the argument is used in this substitu-
tion process. If an argument in a method invocation is a variable (such as year in
Display 4.4), it is the value of the variable that is plugged in for its corresponding
parameter; it is not the variable name that is plugged in . For example, in Display 4.4 ,
the value of the variable year (that is, 1882 ) is plugged in for the parameter newYear .
The variable year is not plugged into the body of the method setDate . Because only
the value of the argument is used, this method of plugging in arguments for formal
parameters is known as the call-by-value mechanism. In Java, this is the only method
of substitution that is used with parameters of a primitive type, such as int , double ,
and char . As you will eventually see, this is, strictly speaking, also the only method
of substitution that is used with parameters of a class type. However, there are other
differences that make parameters of a class type appear to use a different substitution
mechanism. For now, we are concerned only with parameters and arguments of
primitive types, such as int , double , and char . (Although the type String is a class
type, you will not go wrong if you consider it to behave like a primitive type when an
argument of type String is plugged in for its corresponding parameter. However, for
most class types, you need to think a bit differently about how arguments are plugged
in for parameters. We discuss parameters of a class type in Chapter 5.)
In most cases, you can think of a parameter as a kind of blank, or placeholder, that is
filled in by the value of its corresponding argument in the method invocation. However,
parameters are more than just blanks; a parameter is actually a local variable. When the
method is invoked, the value of an argument is computed, and the corresponding
parameter, which is a local variable, is initialized to this value. Occasionally, it is useful
to use a parameter as a local variable. An example of a parameter used as a local variable
is given in Display 4.6. In that display, notice the parameter minutesWorked in the
method computeFee . The value of minutesWorked is changed within the body of the
method definition. This is allowed because a parameter is a local variable.
call-by-value
parameters
as local
variables
 
Search WWH ::




Custom Search