Learn how to add comments in java codes in a single line and multiline. Put Java comments on your java coding to add more information about the working of codes to understand easily. Adding comments to your Java programming is the best practice to design codes.
To make your code more manageable and easily understandable for future use, you must add more comments in each line of code. Commenting out a block of code or a single line of code is the easiest task by using the below syntax for each type of comments.
How to Use Java Comments to Add Comments in Java Coding
There are various types of comments like single, multiple, block and documentation comments. These comments can be used in java programming to implement and add the comments create good codes.
The types of comments are given below.
- Block Comments
- Single Line Comments
- Multi-line Comments
- End of Line Comments
- Documentation Comments
Block Comments in Java
Block comments can be used in java files to give information about the file and its working codes. You can add a block of comments at the beginning of each file. However, you can also add it to any methods and functions to gives overall information about the working of them.
These comments are used to describe any document of java. You can add the working of the file codes in the description part at the start of the file. This makes your code more useful and files are easily manageable. On the opening of the files, you just need to see the block of comment to understand the function and working of the file for the given project
1 2 3 |
/* *Enter block comment here */ |
Example1
1 2 3 4 5 6 7 8 9 10 11 |
/* *File name: MySimpleProgram.java *Author: Roshan Singh Parihar *Description: This contains the simple example of java to print a number. */ public class BlockEx{ public static void main(String args[]){ int i=12; System.out.println(i); } } |
Output
The above code prints only the output of the code and not the comments. Comments are the line which will not get printed in the output, these are only means to understand the working of the code or line of code.
Single Line Java Comments
A single line of comments can be add in java codes to give information of one line code or for few lines of codes. You have to use the symbol to//
add a single line of code to your java programming.
However, this adds a single line comment but still can give more information to understand the code. You can add as many single lines of comment as possible, this is the best practice to create applications.
Syntax
1 |
//Enter single lime comment here |
Example2
1 2 3 4 5 6 7 |
public class BlockEx{ public static void main(String args[]){ //This is a simple java example to print number int i=12; System.out.println(i); } } |
Output
Multiline Java Comments
A multiline comment starts from /*
and ends at */
. You can add as many lines of comments you want inside these symbols. There is no limitation to place this comment to your codes. Many developers using this to comment out a large number of code inside a single file.
However, commenting out a number of codes is not a good practice. You can comment single line as well multiline using this comment system.
Syntax
1 2 3 4 5 |
/* Enter your multiiline comment here */ |
Example3
1 2 3 4 5 6 7 8 9 10 |
public class BlockEx{ public static void main(String args[]){ /* initialize i with 12 and print the number in the next line */ int i=12; System.out.println(i); } } |
Output
End of Line Java Comments
Place comments to the end of any line you want. You just need to put the symbol //
before the comment. See the syntax below to place your comment after line of code.
Put information about the working of the line at the end using the end of line comment system.
Syntax
1 |
This is code //Enter end of lime comment here |
Example4
1 2 3 4 5 6 |
public class BlockEx{ public static void main(String args[]){ int i=12; System.out.println(i); //This prints the number } } |
Output
Documentation Comments in Java
The documentation comments are used in java codes and create documentation API. In order to create a documentation API, you need to use Javadoc tool. To create a doc comment, you have to use the start code/**
and the end code */
.
Syntax
1 2 3 4 5 |
/** Enter your documentation code chere */ |
Example5
1 2 3 4 5 6 |
public class BlockEx{ public static void main(String args[]){ int i=12; System.out.println(i); //This prints the number } } |
Output
You must also read.
Reference.