The Java Tutorials have been written for JDK 8.Java教程是为JDK 8编写的。Examples and practices described in this page don't take advantage of improvements introduced in later releases and might use technology no longer available.本页中描述的示例和实践没有利用后续版本中引入的改进,并且可能使用不再可用的技术。See Java Language Changes for a summary of updated language features in Java SE 9 and subsequent releases.有关Java SE 9及其后续版本中更新的语言特性的摘要,请参阅Java语言更改。
See JDK Release Notes for information about new features, enhancements, and removed or deprecated options for all JDK releases.有关所有JDK版本的新功能、增强功能以及已删除或不推荐的选项的信息,请参阅JDK发行说明。
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 “Hello World!”应用程序由三个主要组件组成:源代码注释、HelloWorldApp
class definition, and the main
method.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.下面的解释将为您提供对代码的基本理解,但更深层次的含义只有在您读完本教程的其余部分后才会显现出来。
/** * 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 */
/*
to */
./*
到*/
的所有内容。/** documentation */
/*
and */
./*
和*/
的注释一样。javadoc
tool uses doc comments when preparing automatically generated documentation.javadoc
工具在准备自动生成的文档时使用文档注释。javadoc
, see the Javadoc™ tool documentation .javadoc
的更多信息,请参阅《Javadoc™工具文档》。// text
//
to the end of the line.//
到行尾的所有内容。HelloWorldApp
Class DefinitionHelloWorldApp
类定义/** * 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.现在,知道每个应用程序都从类定义开始就足够了。
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 在Java编程语言中,每个应用程序必须包含一个main
method whose signature is: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.public
和static
可以按任意顺序编写(public static
或static 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”)。