Comments in Java

Module 1. Java Syntax
Level 2 , Lesson 2
Available

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.


Comments (33)
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION
Danteleaf Level 3, Vietnam
21 February 2024
MAGIC I TOUCHED
John D Corbeta Jr Level 1, Philippines
11 January 2024
mostly common
Rajesh Yadav Level 17, Delhi , India
28 December 2023
just uncomments these three line you will get your output System.out.print("Happy New"); System.out.print(" "); System.out.println("Year");
29 November 2023

//no comment can solve this
nikkehtine Level 2
12 November 2023
Pro tip: use Ctrl + / to quickly toggle comments
28 October 2023
Hi, i am new here
Sanjay Madanu Level 2, India
1 October 2023
i am not getting code
Nandakishor P Level 2, India
23 September 2023
The prgm is actually confusing
ralph magayon Level 2, Philippines
16 September 2023
yeahh i agree
alepd Level 3, United States
30 May 2023
these are veey funny indeed :D.