Program To Display Comments In Java

Hi everyone!! Welcome to flower brackets blog . In this post we are going to learn program to display comments in java.

This is going to be a brief explanation about comments in java.

Comments In Java

So when you are writing a java code or written already you want to document what are you doing.

If there is anything that is confusing in your code, ideally you would rewrite the code to make it not confusing.

But sometimes it helps to leave yourself some notes in the code so that when you go back later and look at it you can understand what happened.

Also Read – Learn Operators In Java

Now these notes are called comments and they do not affect the actual program at all. If you add a comment or remove a comment it does not have any affect on the program that the compiler is going to make it.

It does not have any effect on how the program runs. They are simply there to help programmers understand what you have written.

That being said there is two types of comments in java they are,

  1. Single line comments
  2. Multi line comments

Program To Display Comments In Java

public class CommentsExample
   public static void main(String[] args)

      System.out.println("Here's the single line comment");
      // Hey I'm single line comment      

      System.out.println("Here's the multi line comment");

      /* Hey there!!   
      I'm multi line
      comment  */

Single comment is as shown above. There are two forward slashes and then some text.

Also we have multi line comments that start with a forward slash and then star and some text and end with a star and forward slash.

If you need a quick comment use the single line one but as soon as you hit return and go to a new line it’s not the comment anymore.

The second type of comment is the multi line comment. Like I said it starts with forward slash star and ends with star forward slash.

It’s sort of convention or common practice to put the stars at the beginning of each line of the comment. So it is very clear that it’s a comment.

We will run the above example,

Program To Display Comments In Java

You can see both sys.out will run. But nothing happens to the program because of the two comments.

If you delete above comments, even then the program will run.

One final note about the single line comments is they don’t have to start at the beginning of the line.

For example:

System.out.println("This line is below single line comment");  // Hey I'm single line comment

You can put comment after a line ends. And the comment will be everything from where the forward slash start to the end of the line.

If you run the above example there will be no change in the output.


So that’s it for the post. I hope you have understood the concept to display comments in java. Subscribe to flower brackets blog and share this post across social networks.

You May Also Like