双语版Java程序设计
上QQ阅读APP看本书,新人免费读10天
设备和账号都新为新人

2.7 Comments

Comments are very important (and often neglected). The primary purpose of a comment is to provide additional information to a person that is reading a program. As far as the computer is concerned, a comment is just like more white space. The comment does not result in a token, it only separates other tokens or gets ignored completely when it is not needed to separate tokens. Java has 3 ways to specify comments.

2.7.1 Single Line

Typing // causes the rest of the line (all characters up to the next newline) to be treated as a comment and ignored by the compiler. This is called a single line comment because the comment cannot be longer than a single line. By definition, the comment ends at the end of the line containing the //.

单行的注释用//。

2.7.2 Multi-line

As the names suggests, this style of comment can extend across several lines in a program. The comment beginning is marked with /* and the comment ending is marked with */. Everything between the marks and the marks themselves is a comment and is ignored. Here is the multi-line comment from our first Java program.

多行的注释用/* */。

/* HelloWorld.java
 * Purpose:
 *This program is the classic "Hello World" program for Java. It simply prints a message to the screen.
 */

The single asterisks on the 2nd through 6th lines are not required and are just used to accent the extent of the comment. These comments are also called block comments.

2.7.3 Javadoc

The third style of comment is a minor variation on the previous multi-line comment. The comment beginning marker has an additional asterisk. These comments are identical to the multi-line comment except that they are recognized by a special program called “javadoc” that will automatically extract such comments and produce documentation for the program organized as an HTML document. HTML stands for Hyper Text Markup Language and it is the formatting language used for World Wide Web documents.

javadoc注释是一种多行注释,以/**开头,以*/结束,注释可以包含一些HTML标志符和专门的关键词。

/** starts with slash DOUBLE asterisk and
   continues
   until asterisk slash
*/