Comments
注释
Comments in Java, like comments in most programming languages, do not show up in the executable program. Thus, you can add as many comments as needed without fear of bloating the code. Java has three ways of marking comments. The most common method is a //. You use this for a comment that will run from the // to the end of the line.
Java中的注释像大多数编程语言的注释一样,并不会在可执行程序中出现。因此,你可以需要多少注释就加多少,而不用担心使代码过于臃肿。Java有三种标记注释的方式。最普通的方法是用//。你用它作注释时,从//一直到这一行的结尾都会被当作注释处理。
System.out.println("We will not use 'Hello, World!'"); // is this too cute?
When longer comments are needed, you can mark each line with a //. Or you can use the /* and */ comment delimiters that let you block off a longer comment. This is shown in.
当你需要比较长的注释时,可以在每一行的开头用//来标记。或者你可以用注释分隔符/*和*/来分隔较长的注释。如Example 3-1所示。
Example 3-1. FirstSample.java
1. /*
2. This is the first sample program in Core Java Chapter 3
3. Copyright (C) 1997 Cay Horstmann and Gary Cornell
4. */
5.
6. public class FirstSample
7. {
8. public static void main(String[] args)
9. {
10. System.out.println("We will not use 'Hello, World!'");
11. }
12. }
Finally, a third kind of comment can be used to generate documentation automatically. This comment uses a /** to start and a */ to end. For more on this type of comment and on automatic documentation generation, see Chapter 4.
最后,第三种注释方式可以被用来自动生成文档。这种注释以/**开头,以*/结尾。关于这种注释方式及自动生成文档的更多信息,参见第4章。
CAUTION
/* */ comments do not nest in Java. That is, you cannot deactivate code simply by surrounding it with /* and */ because the code that you want to deactivate might itself contain a */ delimiter.
Java中的/* */注释不能嵌套。也就是说,你不能通过直接用/*和*/把代码围起来的办法使它失效,因为你打算围起来的这段代码本身可能就包含有*/分隔符。