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.

Build an AI-First Career, Master the Complete Skillset

Choose from our industry-leading programs designed for career success

NSDC Certified

Modern Software and AI Engineering Program

Master full-stack development with AI integration

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Modern Data Science and ML with specialisation in AI

Advanced data science techniques with AI specialization

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Advanced AIML with Specialisation in Agentic AI

Deep dive into AIML with focus on Agentic systems

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

DevOps, Cloud & AI Platform Engineering

Build and manage AI-powered cloud infrastructure

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

AI Engineering Advanced Certification by IIT-Roorkee

Premier AI engineering certification from IIT-Roorkee

3 MonthsDuration
AI-LedCurriculum
Career SupportSupport
Program highlights
Go to Program
NSDC Certified

AI Forward Deployed Engineer Program

Full-stack engineering, production AI and client-facing consulting

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program

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
Sharpen Your Fundamentals with Free Learning

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 :

How Scaler Transformed Careers in Different Fields

₹23L
AVG CTC
SCALER PLACEMENT PROOF

Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.

11,000+placements
650+companies
Verified data
Hiring Partners:
GoogleGoogleAmazonAmazonMicrosoftMicrosoftFlipkartFlipkartAdobeAdobe1200+ more

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:

Turn Learning into Career Growth

1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary
1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary

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.