![](https://csdnimg.cn/release/download_crawler_static/9031627/bg12.jpg)
10 CHAPTER 1: Getting Started with Java
The main() method includes a header that identifies this method and a block of code located
between an open brace character ({) and a close brace character (}). Besides naming this method,
the header provides the following information:
public: This reserved word makes main() visible to the startup code that calls
this method. If public wasn’t present, the compiler would output an error
message stating that it couldn’t find a main() method.
static: This reserved word causes this method to associate with the class
instead of associating with any objects (discussed in Chapter 3) created from
this class. Because the startup code that calls main() doesn’t create an object
from the class to call this method, it requires that the method be declared
static. Although the compiler will not report an error when static is missing,
it will not be possible to run HelloWorld, which will not be an application when
the proper main() method doesn’t exist.
void: This reserved word indicates that the method doesn’t return a value. If you
change void to a type’s reserved word (such as int) and then insert code that
returns a value of this type (such as return 0;), the compiler will not report an
error. However, you won’t be able to run HelloWorld because the proper main()
method wouldn’t exist. (I discuss types in Chapter 2.)
(String[] args): This parameter list consists of a single parameter named
args, which is of type String[]. Startup code passes a sequence of command-
line arguments to args, which makes these arguments available to the code
that executes within main(). You’ll learn about parameters and arguments in
Chapter 3.
main() is called with an array of strings (character sequences delimited by double quote "
characters) that identify the application’s command-line arguments. These strings are stored in
String-based array variable args. (I discuss method calling, arrays, and variables in Chapters 2 and
3.) Although the array variable is named args, there’s nothing special about this name. You could
choose another name for this variable.
main() presents a single line of code, System.out.println("Hello, World!");, which is responsible
for outputting Hello, World! in the command window from where HelloWorld is run. From left to
right, this method call accomplishes the following tasks:
System identifies a standard class of system utilities.
out identifies an object variable located in System whose methods let you output
values of various types optionally followed by a newline (also known as line feed)
character to the standard output stream. (In reality, a platform-dependent line
terminator sequence is output. On Windows platforms, this sequence consists
of a carriage return character [integer value 13] followed by a line feed character
[integer value 10]. On Linux platforms, this sequence consists of a line feed
character. On Mac OS X systems, this sequence consists of a carriage return
character. It's convenient to refer to this sequence as a newline.)
println identifies a method that prints its "Hello, World!" argument (the
starting and ending double quote characters are not written; these characters
delimit but are not part of the string) followed by a newline to the standard
output stream.
www.it-ebooks.info