1. 2種類のコメント

上で述べたことにいくつかの言葉を追加する必要があります。

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");
   }
}

「今度はフレーズを表示します…」というコメントを追加しました。コメントの始まりは記号のペア ( /*) で示され、コメントの終わりは ( */) で示されます。プログラムがコンパイルされると、コンパイラーはシンボル/*と記号の間のすべてを省略します。*/

コメント内には何でも書き込めます。

通常、コード内のコメントは、コードの理解しにくい部分に関するものです。一部のコメントは数十の文字列で構成されています。これらは、メソッドの動作方法のニュアンスを説明するためにメソッドの前に書かれることがよくあります。

コードにコメントを追加する方法がもう 1 つあります。2 つのスラッシュ ( ) を使用できます//

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 番目の記号のペアはありません。


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.

はい、いくつかのコメントはとても面白いです。