1. Dos tipos de comentarios

Deberíamos añadir algunas palabras a lo dicho anteriormente.

En Java, puede escribir comandos, pero también puede agregar comentarios a esos comandos directamente en el código. El compilador ignora por completo los comentarios. Cuando se ejecuta el programa, se omiten todos los comentarios.

Aquí hay un ejemplo:

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

Agregamos el comentario 'Ahora mostraremos la frase...'. El comienzo del comentario se indica con un par de símbolos ( /*), y el final, con ( */). Cuando se compila el programa, el compilador omite todo entre los símbolos /*y*/

Puedes escribir lo que quieras dentro de un comentario.

Por lo general, los comentarios en el código se refieren a partes del código que son difíciles de entender. Algunos comentarios constan de docenas de cadenas: a menudo se escriben antes de los métodos para describir los matices de cómo funcionan.

Hay una forma más de agregar un comentario al código. Puede utilizar dos barras diagonales ( //).

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

Aquí, el código que comienza con el //y hasta el final de la línea con el //se considera un comentario. En otras palabras, no se usa un segundo par de símbolos para 'completar el comentario'.


2. Comentarios de la vida de los programadores

Por cierto, algunos comentarios son muy interesantes.

// 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.

Sí, algunos comentarios son muy graciosos.