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

Example1

Output

12

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

Example2

Output

12

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

Example3

Output

12

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

Example4

Output

12

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

Example5

Output

12

You must also read.

Reference.

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.