Table of Contents
Comments
are the essential part of code. If you write good comments, it will be easier for others to understand the code. Comments are ignored by the compiler when it compiles the program.
There are three types of comments in java code.
- Single line comments
- Multi-line comments
- Java doc comments
Single line comments:
These are most used java comments. It is used to comment a single line.
1 2 3 |
// This is single line comment |
For example:
1 2 3 4 5 6 |
public class CommentsExample { int count; // Here count is a variable } |
Multi line comments:
If you want to write comments in multiple line, single lines comments may be tedious to write.In this situation, you can use multi line comments.
1 2 3 4 5 6 |
/* This is multi line comment * * */ |
For example:
1 2 3 4 5 6 7 8 9 |
public class CommentsExample { /* Count is a variable which is used * to count number of incoming requests */ int count; } |
Javadoc comments:
If you want to generate javadoc for your class, you can use javadoc comments. When you run javadoc command, it automatically generate javadoc with javadoc comments
1 2 3 4 5 6 |
/** This is javadoc comment * * */ |
For example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
public class CommentsExample { /** * This method is used to find sum of two integers * @param a * @param b * @return */ public int sum(int a,int b) { int c=a+b; return c; } } |
Comments are most important part of code. You should use them as and when required.