Core java tutorial for beginners
A tutorial blog which explains different core concepts related to Java along with programming examples

September 25, 2014 Categories: Core Java Basics. No Comments on Java comments

In this article you will learn a lot about Java comments. First we will look at what is a comment, why comments are needed, what are the different types of comments and finally some example programs which will show you how to use comments in Java programs.





Writing comments is considered a good programming practice. They will help the client programmers to understand the programs developed by you.



Java comments can be a single line or multiple lines of text which are written by the programmers for documentation purpose. A programmer can write comments to mention the purpose of the program or a part of the program or to provide authorship details.


Need for comments


Following are some of the famous reasons for writing comments in programs:

  • Provide the purpose of the program along with sample input and output of the program.
  • Provide authorship details.
  • Mention date and time of creation of the program and when it was last modified.
  • Provide details about several parts of the program.
  • For generating documentation which will be helpful for other programmers.


Types of comments


In  Java, there are three types of comments. They are:

  • Single line comments
  • Multi-line comments
  • javadoc comments


A single line comment as the name implies is a comment containing a single line of text. A single line comment starts with //. All the characters after the // to the end of the line is treated as a comment. Syntax for single line comment is as shown below:


// This is a single line comment


To write a comment containing more than one line, we can use a multi-line comment. A multi-line comment starts with /* and ends with */. Syntax for writing a multi-line comment is as shown below:


/*  This is a
comment */


We can use a multi-line comment for writing a single line comment as shown below:


/* This is a single line comment. */


Both single line comments and multi-line comments can be written anywhere in the program.


The third type of comments called javadoc comments can be used to generate HTML documentation which can be viewed using a browser. This can help the client programmers to understand the classes that you create. A javadoc comment starts with /** and ends with */. Syntax for writing a javadoc comment is as shown below:


* This is a
* javadoc comment


Note that * is not necessary before each line of text. It is widely accepted usage of javadoc comments by Java developers. Generally javadoc comments are written at the top of the program. Comments which are written before a class declaration are known as header comments.


We can mention several types of specific information like author name, parameters of a function, return value of a method and many more using a javadoc comment. For complete information regarding the javadoc comment and its usage refer this link.


Example program using all the three types of comments:



One thing to remember about comments is, they cannot be nested. For example, the following is a incorrect way of writing comments:


//This is a //bad single line comment


Every comment started using a beginning marker must be closed with a ending marker. Following is an example of wrong way of writing comments:


This is
a bad
comment /


Note that in the above example, the ending marker should be */.

Related Links:

Note: Do you have a question on this article or have a suggestion to make this article better? You can ask or suggest us by filling in the below form. After commenting, your comment will be held for moderation and will be published in 24-48 hrs.

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll Up