Lesson: A Closer Look at the "Hello World!" Application
Now that you've seen the "Hello World!" application (and perhaps even compiled and run it), you might be wondering how it works. Here again is its code:The "Hello World!" application consists of three primary components: source code comments, the/**
* 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.
}
}HelloWorldApp
class definition, and themain
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.
Source Code Comments
The following bold text defines the comments of the "Hello World!" application:/**
* 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.
}
}
/* text */
- The compiler ignores everything from
/*
to*/
.
/** documentation */
- This indicates a documentation comment (doc comment, for short). The compiler ignores this kind of comment, just like it ignores comments that use
/*
and*/
. Thejavadoc
tool uses doc comments when preparing automatically generated documentation. For more information onjavadoc
, see the JavadocTM tool documentation .
// text
- The compiler ignores everything from
//
to the end of the line.
The HelloWorldApp
Class Definition
The following bold text begins the class definition block for the "Hello World!" application:/**
* 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 main
Method
The following bold text begins the definition of themain
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.
}
}In the Java programming language, every application must contain a
main
method whose signature is:The modifierspublic static void main(String[] args)public
andstatic
can be written in either order (public static
orstatic public
), but the convention is to usepublic static
as shown above. You can name the argument anything you want, but most programmers choose "args" or "argv".The
main
method accepts a single argument: an array of elements of typeString
.This array is the mechanism through which the runtime system passes information to your application. 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. For example, a sorting program might allow the user to specify that the data be sorted in descending order with this command-line argument:public static void main(String[] args)-descendinguses theSystem.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.Questions
1. When you compile a program written in the Java programming language, the compiler converts the human-readable source file into platform-independent code that a Java Virtual Machine can understand. What is this platform-independent code called?2. Which of the following is not a valid comment:
a.3. What's the first thing you should check if you see the following error at runtime:/** comment */
b./* comment */
c./* comment
d.// comment
Exception in thread "main" java.lang.NoClassDefFoundError: HelloWorldApp.java.
4. What is the correct signature of the
main
method?5. When declaring the
main
method, which modifier must come first,public
orstatic
?6. What parameters does the
main
method define?Exercises
1. Change theHelloWorldApp.java
program so that it displaysHola Mundo!
instead ofHello World!
.2. You can find a slightly modified version of
HelloWorldApp
here:HelloWorldApp2.java
The program has an error. Fix the error so that the program successfully compiles and runs. What was the error?
No comments:
Post a Comment