1.兩種評論

我們應該在上面說的基礎上再補充幾句。

在 Java 中,您可以編寫命令,但您也可以直接在代碼中為這些命令添加註釋。編譯器完全忽略註釋。當程序運行時,所有註釋都被省略。

這是一個例子:

public class Home
{
   public static void main (String[] args)
   {
      /*
      Now we'll display the phrase 'Amigo is the Best'
      */
      System.out.print("Amigo ");
      System.out.print("is the ");
      System.out.print("Best");
   }
}

我們添加了註釋“現在我們將顯示短語……”。註釋的開頭由一對符號 ( /*) 表示,結尾由 ( */) 表示。編譯程序時,編譯器會忽略符號/*和符號之間的所有內容*/

你可以在評論中寫任何你想寫的東西。

通常,代碼中的註釋是關於代碼中難以理解的部分。一些評論由幾十個字符串組成:這些通常寫在方法之前,以描述它們如何工作的細微差別。

還有一種方法可以為代碼添加註釋。您可以使用兩個正斜杠 ( //)。

public class Home
{
   public static void main (String[] args)
   {
      System.out.print("Amigo ");
      System.out.print("is the "); // This is also a comment
      System.out.print("Best");
   }
}

在這裡,以//和到行尾的代碼//被認為是註釋。換句話說,沒有第二對符號用於“完成評論”。


2.程序員生活感言

順便說一句,有些評論真的很有趣。

// I'm not responsible for this code. I was forced to write it against my will.
// Dear, future me. Please forgive me for this code.
// If I see something like this once more, I'll have a complete mental breakdown at work.
// If this condition is ever satisfied, please inform me for a reward. Phone: xxx-xxx-xxx.
//
// Dear programmer:
//
// When you finish 'optimizing' this subroutine
// and realize what a huge mistake it was,
// please increment the following counter as a warning
// to the next guy:
//
// total_hours_wasted_here = 42
//
// When I wrote this, only God and I understood what I was doing
// Now only God knows.
// Sometimes it seems that the compiler is just ignoring all my comments
// I dedicate all my code and my work to my wife Darlene,
// who will have to provide for me, our three kids, and the dog when
// it gets released to the public.
// Magic. Don't touch.

是的,有些評論很有趣。