1. Two kinds of comments
We should add a few words to what was said above.
In Java, you can write commands, but you can also add comments to those commands right in the code. The compiler completely ignores the comments. When the program is run, all the comments are omitted.
Here's an example:
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");
}
}
We added the comment 'Now we'll display the phrase…'. The beginning of the comment is indicated by a pair of symbols (/*
), and the end – by (*/
). When the program is compiled, the compiler omits everything between the symbols /*
and */
You can write whatever you want inside a comment.
Usually, comments in code are about parts of the code that are difficult to understand. Some comments consist of dozens of strings: these are often written before methods to describe nuances in how they work.
There is one more way to add a comment to code. You can use two forward slashes (//
).
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");
}
}
Here, the code starting with the //
and up to the end of the line with the //
is considered to be a comment. In other words, there is no second pair of symbols used to 'complete the comment'.
2. Comments from the life of programmers
By the way, some comments are really interesting.
// 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.
Yes, some comments are very funny.
GO TO FULL VERSION