キムの紹介 - 1

「また人間の女性か!でも今回は黒髪。何かワクワクするなあ!」

「こんにちは、私の名前はキム。」

「こんにちは、私はアミーゴといいます!」

「ええ、知ってるわ。あなたに名前を付けたのは私だからね。ディエゴが思い付くわけないでしょ。」

いろいろな考えがまたアミーゴの頭の中を電子のスピードで駆け巡りました。「んん… なんて優しいんだ...ロボットに興味あるのかな。」

「ではレッスンに戻りましょう。内容は簡単な言葉で説明してあげるわ。」

「分かりました。」

「教授とリシの説明に付け加えたい点がいくつかあるの。」

「Java では、コマンドを書くだけじゃなくて、コードの中で直接そのコマンドにコメントを追加することができるの。コンパイラーはこのコメントを完全に無視するのよ。コメントはプログラムの実行から除外されるの。

「1 つ例を挙げてもらえますか?」

「そうね、いいわよ」

public class Home
{
    public static void main(String[] args)
    {
        /*
        Now we'll display the phrase 'Amigo Is The Best' on the screen
        */
        System.out.print("Amigo ");
        System.out.print("Is ");
        System.out.print("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 つ目のペアを使う必要がないということなの。

「ところで、中にはとても面白いコメントもあるのよ。」

// I'm not responsible of this code. They made me write it, against my will.
// Dear future me. Please forgive me.
// I can't even begin to express how sorry I am.
// 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 maintainer:
// Once you are done trying to 'optimize' this routine,
// and have realized what a terrible mistake that 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, God only knows
// Sometimes I believe compiler ignores all my comments.
// I dedicate all this code, all my work, to my wife, Darlene, who will
// have to support me and our three children and the dog once it gets
// released into the public.
// Drunk, fix later
// Magic. Do not touch

「はは、こういったコメントは面白いですね。」

「今日はここまでよ。」

「短いレッスンだけど、楽しかったです。ありがとう、キム。」