Types of Comments in Java

Java Comments
Java Comments
First of all a comment is a description about the code we write and a comment will not be executed at all.

So there are three types of comments in java.

1.Single Line Comments
2.Multi Line Comments
3.Javadoc Comments

Now i will explain it in detail :

1.Single Line Comments

  • Whenever we want to write a comment that uses or needs only a single line then we use this type comments.
  • For this we use two forward slashes without space (//) before we start our comment.
  • We can also comment multiple lines using this, but we should use double forward slash (//) for each line, that can become an inconvenience.
Code example :

1
2
3
4
5
public class Sample{
 //Line Comments
    //This is a single Line comment
 
}

2.Multi Line Comments
  • When comment needs more than single lines like a paragraph.
  • Here multiple lines of comment is enclosed between forward slash then a star symbol (/*) and the comment block is closed by a star symbol then a forward slash(*/).
  • This type of comment is the only good choice when we needed to insert lot of lines of comment in the middle of code.
Code example:

1
2
3
4
5
6
7
public class Sample{
 /*Multi Line Comments*/
 
 /*This comments can span
   multiples lines as
   shown here.*/
}

3.Javadoc Comments

  •  This kind of comment is called java documentation comment.
  •  Usually the API's of a class are documented in this way.
  •  These comments are used to generate a HTML web page documentation from the comments of the written code using the tool called Javadoc. This means the comments we write with this type can be easily converted into a separate webpage for documentation purpose.
  •  Here multiple lines of comment is enclosed between forward slash then two star symbols (/**) and end with a star symbol and forward slash (*/).Note there is one more star at the beginning compared with multi line comments.

Code Example:

1
2
3
4
5
6
/** This is a javadoc comment used for documentation
    purposes.Note there is one more star symbol at the beginning. */
public class Sample{

 private int demoVariable;
}
Share on Google Plus

About Vaishakh

I am a Java and Android developer and a science enthusiast.I Like to explain complex things in simple language so anybody even without a graduation in computer science or Electronics can understand the concepts easily.

0 comments:

Post a Comment