Java Comments

Learn via video course
FREE
View all courses
Java Course - Mastering the Fundamentals
Java Course - Mastering the Fundamentals
by Tarun Luthra
1000
5
Start Learning
Java Course - Mastering the Fundamentals
Java Course - Mastering the Fundamentals
by Tarun Luthra
1000
5
Start Learning
Topics Covered

Comments in Java enhance code readability by providing non-executable statements, particularly useful for understanding complex programs.

Why Do We Use Comments in Code?

  • By using comments, a program can be made more readable by adding details to every code snippet.
  • It is used to ease the debugging of the code.
  • Used for providing explanation or information about a particular variable or method or logic.
  • While testing some alternative code, it is used for preventing the execution of a program.

Types of Comments in Java

There are three types of comments in Java:**

  1. Java Single Line Comments
  2. Java Multi-Line Comments
  3. Java Documentation Comments

Java Single-line Comments

It is the easiest comment that beginners mostly use for describing the functionality of code. It is used when we have to comment on only one line of the code.

It is done by putting two forward slashes at the front of the text or line. Any text before it is not commented on. Any text written after the "//" is commented on.

Syntax :

For example :

Java Multi-line Comments

A "single line comment" can be tedious to use when explaining a complex program in a detailed manner. This is where "multi-line comments" are used. It is used for explaining complex code in a detailed manner as well as commenting on multiple lines of the code at the same time.

Multi-line comments are put into:

Syntax :

Example:

Java Documentation Comments

These are usually used for large programs/projects or software applications for maintaining documentation of the API for reference. These APIs usually consist of classes, methods, and arguments used in the code.

Syntax:

Example:

Javadoc Tags

Here are some of the most commonly used tags for documentation purposes.

tagsyntaxdescription
{@docRoot}{@docRoot}It represents the path of the root directory to generate a document of any page.
@author@author name - textAdding class author
@code{@code text}To display text by using code font without interrupting HTML markup
@version@version version-textUsed for specifying “version” subheading and text
@since@since releaseUsed for adding “since” heading and text for generating documentation.
@param@param parameter-name descriptionAdding a parameter with a given name to the “parameter” section.
@return@return descriptionThis is required for every method as it returns the output, used for every method except the void.

Java Program:

By compiling the above program with javadoc Calculate.java, the HTML files are created for the Calculate class in the current directory. Open the HTML files, and we can see the explanation of Calculate class provided through the documentation comment.

Conclusion

  • Comments in Java, including single-line, multi-line, and documentation comments, play a crucial role in enhancing code readability and understanding.
  • Single-line comments in Java, denoted by "//", are useful for brief explanations or comments on a single line of code.
  • Multi-line comments in Java, enclosed within "/" and "/", are preferred for explaining complex logic or commenting on multiple lines at once.
  • Documentation comments, commonly referred to as Javadoc comments, are essential for generating API documentation and providing structured information about classes, methods, and parameters.
  • Javadoc comments utilize specific tags such as "@param" and "@return" to document parameters, return values, and other relevant information for methods.
  • By incorporating Javadoc comments effectively, developers can ensure comprehensive documentation, aiding in code maintenance and collaboration.
  • Overall, the strategic use of comments in Java significantly contributes to code comprehension, maintenance, and collaboration among developers.