class HelloWorldApp { public static void main(String[] args) { System.out.println("Hello World!"); // Display the string. } }
HelloWorldApp
class definition, and the main
method.
The following explanation will provide you with a basic understanding
of the code, but the deeper implications will only become apparent
after you've finished reading the rest of the tutorial.
/** * The HelloWorldApp class implements an application that * simply prints "Hello World!" to standard output. */ class HelloWorldApp { public static void main(String[] args) { System.out.println("Hello World!"); // Display the string. } }
Comments are ignored by the compiler but are useful to other programmers. The Java programming language supports three kinds of comments:
/* text */
/*
to
*/
.
/** documentation */
/*
and
*/
.
The javadoc
tool uses doc comments
when preparing automatically generated documentation.
For more information on javadoc
, see the
Javadoc™ tool documentation .
// text
//
to the end of the line.
HelloWorldApp
Class Definition/** * The HelloWorldApp class implements an application that * simply displays "Hello World!" to the standard output. */ class HelloWorldApp { public static void main(String[] args) { System.out.println("Hello World!"); // Display the string. } }
As shown above, the most basic form of a class definition is:
class name { . . . }
The keyword
In the Java programming language, every application
must contain a
The
The
The "Hello World!" application ignores its command-line arguments,
but you should be aware of the fact that such arguments do exist.
Finally, the line:
class
begins the class definition
for a class named name
,
and the code for each class appears between the opening and closing curly
braces marked in bold above. Chapter 2 provides an overview of classes in general, and Chapter 4 discusses classes in detail. For now it is enough to know that every application begins with a class definition.
The
The following bold text begins
the definition of the main
Methodmain
method:
/**
* The HelloWorldApp class implements an application that
* simply displays "Hello World!" to the standard output.
*/
class HelloWorldApp {
public static void main(String[] args) {
System.out.println("Hello World!"); //Display the string.
}
}
main
method whose signature is:
public static void main(String[] args)
public
and static
can
be written in either order (public static
or static public
), but the convention
is to use public static
as shown above.
You can name the argument anything you want, but most programmers choose "args"
or "argv".
main
method
is similar to the main
function in C and C++;
it's the entry point for your application
and will subsequently invoke all the other methods required by your program.
main
method accepts a single argument:
an array of elements of type String
.
public static void main(String[] args)
java MyApp arg1 arg2
-descending
System.out.println("Hello World!");
System
class from the core library to print the "Hello World!" message to standard output. Portions of this library (also known as the "Application Programming Interface", or "API") will be discussed throughout the remainder of the tutorial.