Documentation

The Java™ Tutorials
Hide TOC
A Closer Look at the "Hello World!" Application
Trail: Getting Started

Lesson: A Closer Look at the "Hello World!" Application课程:仔细看看“Hello World!”应用

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:下面是它的代码:

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.“Hello World!”应用程序由三个主要组件组成:源代码注释HelloWorldApp类定义main方法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:以下粗体文本定义了“Hello World!”应用程序:

/**
 * 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:Java编程语言支持三种注释:

/* 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 */.编译器忽略此类注释,就像它忽略使用/**/的注释一样。The javadoc tool uses doc comments when preparing automatically generated documentation.javadoc工具在准备自动生成的文档时使用文档注释。For more information on javadoc, see the Javadoc™ tool documentation .有关javadoc的更多信息,请参阅《Javadoc™工具文档》。
// text
The compiler ignores everything from // to the end of the line.编译器忽略从//到行尾的所有内容。

The HelloWorldApp Class DefinitionHelloWorldApp类定义

The following bold text begins the class definition block for the "Hello World!" application:以下粗体文本开始“Hello World!”的类定义块应用程序:

/**
 * 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 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.关键字class开始名为name的类的类定义,每个类的代码出现在上面粗体标记的开始和结束大括号之间。Chapter 2 provides an overview of classes in general, and Chapter 4 discusses classes in detail.第2章概括介绍了类,第4章详细讨论了类。For now it is enough to know that every application begins with a class definition.现在,知道每个应用程序都从类定义开始就足够了。

The main Methodmain方法

The following bold text begins the definition of the main method:以下粗体文本开始定义main方法:

/**
 * 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:在Java编程语言中,每个应用程序必须包含一个main方法,其签名为:

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.修饰符publicstatic可以按任意顺序编写(public staticstatic public),但约定使用public static,如上所示。You can name the argument anything you want, but most programmers choose "args" or "argv".您可以随意命名参数,但大多数程序员选择“args”或“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.main方法与C和C++的main函数相似;它是应用程序的入口点,随后将调用程序所需的所有其他方法。

The main method accepts a single argument: an array of elements of type String.main方法接受一个参数:String类型的元素数组。

public static void main(String[] args)

This array is the mechanism through which the runtime system passes information to your application.此数组是运行时系统将信息传递给应用程序的机制。For example:例如:

java MyApp arg1 arg2

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:例如,排序程序可能允许用户使用以下命令行参数指定数据按降序排序:

-descending

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.使用核心库中的System类打印“Hello World!”消息到标准输出。Portions of this library (also known as the "Application Programming Interface", or "API") will be discussed throughout the remainder of the tutorial.本教程的其余部分将讨论该库的部分内容(也称为“应用程序编程接口”或“API”)。


Previous page: Previous Lesson
Next page: Questions and Exercises: Getting Started