still compile your program. However, java would report an error because it would be unable
to find the main( ) method.
Any information that you need to pass to a method is received by variables specified
within the set of parentheses that follow the name of the method. These variables are called
parameters. If there are no parameters required for a given method, you still need to include
the empty parentheses. In main( ), there is only one parameter, albeit a complicated one. String
args[ ] declares a parameter named args, which is an array of instances of the class String.
(Arrays are collections of similar objects.) Objects of type String store character strings. 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.
The last character on the line is the {. This signals the start of main( )'s body. All of the
code that comprises a method will occur between the method's opening curly brace and its
closing curly brace.
One other point: main( ) is simply a starting place for your program. A complex program
will have dozens of classes, only one of which will need to have a main( ) method to get
things started. When you begin creating applets--Java programs that are embedded in web
browsers--you won't use main( ) at all, since the web browser uses a different means of
starting the execution of applets.
The next line of code is shown here. Notice that it occurs inside main( ).
System.out.println("This is a simple Java program.");
This line outputs the string "This is a simple Java program." followed by a new line on the
screen. Output is actually accomplished by the built-in println( ) method. In this case, println( )
displays the string which is passed to it. As you will see, println( ) can be used to display
other types of information, too. The line begins with System.out. While too complicated to
explain in detail at this time, briefly, System is a predefined class that provides access to the
system, and out is the output stream that is connected to the console.
As you have probably guessed, console output (and input) is not used frequently in
most real-world Java programs and applets. Since most modern computing
environments are windowed and graphical in nature, console I/O is used mostly for
simple utility programs and for demonstration programs. Later in this topic, you will learn
other ways to generate output using Java. But for now, we will continue to use the console
I/O methods.
Notice that the println( ) statement ends with a semicolon. All statements in Java end
with a semicolon. The reason that the other lines in the program do not end in a semicolon
is that they are not, technically, statements.
The first } in the program ends main( ), and the last } ends the Example class definition.
A Second Short Program
Perhaps no other concept is more fundamental to a programming language than that of a
variable. As you probably know, a variable is a named memory location that may be assigned
a value by your program. The value of a variable may be changed during the execution of
the program. The next program shows how a variable is declared and how it is assigned a
value. The program also illustrates some new aspects of console output. As the comments
at the top of the program state, you should call this file
Search WWH :
Custom Search
Previous Page
Java SE 6 Topic Index
Next Page
Java SE 6 Bookmarks