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