source: https://docs.oracle.com/javase/tutorial/getStarted/application/index.html
class HelloWorldApp {
public static void main(String[] args) {
System.out.println("Hello World!"); // Display the string.
}
}
The "Hello World!" application consists of three primary components: source code comments, the HelloWorldApp
class definition, and the main
method.
HelloWorldApp
Class DefinitionAs shown above, the most basic form of a class definition is:
class name {
. . .
}
The keyword class
begins the class definition for a class named name
, and the code for each class appears between the opening and closing curly {}
braces. For now it is enough to know that every application begins with a class definition.
main
MethodIn the Java programming language, every application must contain a main
method whose signature is:
public static void main(String[] args)
The modifiers 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".
The 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.
The main
method accepts a single argument: an array of elements of type String
.
Each string in the array is called a command-line argument. Command-line arguments let users affect the operation of the application without recompiling it.
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:
System.out.println("Hello World!");
uses the 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.