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.
当需要更长的注释的时候,你可以用//来注释每一行。或者你也可以使用/*和*/这两个注释定界符来隔离一个较长的注释。如例子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.
最后,第三种注释用于自动生成文档。该注释以/**开始以*/结束。有关这种注释和自动生成文档的更多内容,请见第四章。
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中是不可嵌套的。也就是说,当你想要注释掉一段代码的时候,你不能简单的使用/*和*/,因为你想注释掉的代码可能就含有一个*/定界符。
文章来源:
http://x-spirit.spaces.live.com/Blog/cns!CC0B04AE126337C0!287.entry