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.

是的,有些评论很有趣。