1. Rong jinis komentar

Kita kudu nambah sawetara tembung kanggo apa sing kasebut ing ndhuwur.

Ing Jawa, sampeyan bisa nulis prentah, nanging sampeyan uga bisa nambah komentar ing printah kasebut ing kode kasebut. Compiler rampung nglirwakake komentar. Nalika program diluncurake, kabeh komentar bakal ilang.

Iki contone:

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

Kita nambahake komentar 'Saiki kita bakal nampilake frasa…'. Awal komentar dituduhake dening pasangan simbol ( /*), lan pungkasan - dening ( */). Nalika program wis nyawiji, compiler ngilangi kabeh antarane simbol /*lan*/

Sampeyan bisa nulis apa wae sing dikarepake ing komentar.

Biasane, komentar ing kode babagan bagean kode sing angel dingerteni. Sawetara komentar kalebu puluhan senar: iki asring ditulis sadurunge cara kanggo njlèntrèhaké nuansa ing cara kerjane.

Ana siji cara liyane kanggo nambah komentar menyang kode. Sampeyan bisa nggunakake rong garis miring maju ( //).

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

Ing kene, kode sing diwiwiti saka //lan nganti pungkasan baris karo //dianggep minangka komentar. Ing tembung liyane, ora ana pasangan simbol kapindho sing digunakake kanggo 'ngrampungake komentar'.


2. Komentar saka urip programer

Miturut cara, sawetara komentar menarik banget.

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

Ya, sawetara komentar sing lucu banget.