CoderSathi
  • Tutorial
    • Java Tutorial
    • Swing Tutorial
    • JDBC Tutorial
    • Java String Tutorial
    • Servlet and JSP Tutorial
  • Mongo DB
  • AWS
  • DevOps
  • Linux
  • Git
Home > Java > Comments in Java

Java Tutorial

  • Introduction
    • What is Java
    • History Of Java
    • Install Java
    • What is JVM
    • JDK vs JRE vs JVM
    • Java Bytecode
    • OOP vs POP
    • Compile and Run Java
  • Tokens, Expressions and Control Structures
    • Primitive data types
    • Integers
    • Floating Points
    • Characters
    • Booleans
    • User Defined Data Type
    • Declarations
    • Constants
    • Identifiers
    • Literals
    • Type Conversion and Casting
    • Variables
    • Default Variable Initialization
    • Command Line Arguments
    • Arrays of Primitive Types
    • Comment Syntax
    • Garbage Collection
    • Expressions
    • Operators
    • Arithmetic Operator
    • Bitwise and Shift Operator
    • Comparison or Relational Operators
    • Logical Operators
    • Assignment Operators
    • Ternary Operator
    • Increment and Decrement Operator
    • Control Statements
  • OOP Concepts
    • Class and Object
    • Create Class Instance
    • Method
    • Abstraction
    • Encapsulation
    • this keyword
    • Constructor
    • Pass by Value
    • Access Modifier/Control
    • Polymorphism
    • Method Overloading vs Method Overriding
    • Recursion
    • Nested and Inner Class
  • Inheritance and Packaging
    • Inheritance
    • extends Keyword
    • super Keyword
    • Object Class
    • Abstract class
    • Final Class
    • Java Package
    • Interface
  • Handling Error/ Exceptions
    • What is Exception
    • Exception Handling Keywords
    • Common Java Errors
    • User Defined Exception
    • Throwing and re-throwing Exception
    • finally Block
  • Strings
    • Java String Tutorial
  • Threads
    • Introduction
    • Create Thread
    • Thread Lifecycle
    • Thread Priority
    • Thread Synchronization
    • Inner Thread Communication
    • Thread Deadlock
  • IO and Streams
    • java.io Package
    • Files and Directories
    • Byte Stream
    • Character Stream
    • Console Input and Output
    • Serializable and Deserializable
  • Core Packages
    • java.lang Package
    • Math
    • Wrapper Classes
    • java.lang.Number
    • Double
    • Float
    • Integers
    • java.lang.Byte
    • java.lang.Short
    • java.lang.Long
    • java.lang.Character
    • java.lang.Boolean
    • java.util package
    • Vector Class
    • Stack Class
    • Dictionary Class
    • Hashtable
    • Enumeration or Enum
    • Generate Random Number
  • Holding Collection of Data
    • Arrays
    • Map
    • List
    • Set
    • Collection Interface
    • Collections Class
    • ArrayList
    • HashSet
    • TreeSet
    • Comparator
  • Java Bean
    • What is Java Bean
    • Advantages and Disadvantages of Java Bean
    • Java Beans API
    • Introspection
    • Java Bean Properties
    • Bound and Constrained Properties
    • BeanInfo Interface
    • Customizers
    • Java Beans Persistence
    • BeanDescriptor
  • Home

Comments in Java

Learn the concepts, implementation details, and practical steps with a clean developer-focused walkthrough.

Yuba Raj Kalathoki
By Yuba Raj Kalathoki
Last updated: July 1, 2026 · 4 min read · 0 Comments
Share: in X

Comments are an essential part of any programming language, and Java is no exception. They are used to explain the purpose of the code, make it more readable, and provide documentation for other developers (or our future self). Java supports three types of comments: single-line comments, multi-line comments, and Javadoc comments. In this post, we’ll explore each type of comments in Java, their syntax, and best practices for using them effectively. Whether you’re a beginner or an experienced developer, this guide will help you master the use of comments in Java.

Table of Contents

  • What are Comments in Java?
  • Types of Comments in Java
    • 1. Single-Line Comments
    • 2. Multi-Line Comments
    • 3. Javadoc Comments
  • Best Practices for Using Comments
  • Common Mistakes to Avoid
  • Conclusion
  • Frequently Asked Questions
    • What is the purpose of comments in Java?
    • Can I nest multi-line comments in Java?
    • Is it a good practice to comment every line of code?
    • Do comments affect the performance or size of the compiled Java code?

What are Comments in Java?

Comments are non-executable lines in our code that are ignored by the Java compiler. They are used to:

  • Explain the purpose of the code.
  • Provide documentation for methods, classes, and variables.
  • Temporarily disable code during debugging.

Types of Comments in Java

Java supports three types of comments:

1. Single-Line Comments

Single-line comments are used for short explanations or notes. They begin with // and continue to the end of the line.

Syntax:

// This is a single-line comment

Example:

// This is a single-line comment in Java
int x = 10; // This is another comment after a statement

2. Multi-Line Comments

Multi-line comments are used for longer explanations or to comment out blocks of code. They begin with /* and end with */.

Syntax:

/*
This is a multi-line comment.
It can span multiple lines.
*/

Examples:

/*
  This is a multi-line comment in Java.
  It can span multiple lines.
  Useful for longer explanations or disabling blocks of code.
*/
public int add(int a, int b) {
    return a + b;
}

3. Javadoc Comments

Javadoc comments are used to generate documentation for our code. They begin with /** and end with */. Javadoc comments can include tags like @param, @return, and @throws to provide detailed information about methods, classes, and variables.

Syntax:

/**
 * This is a Javadoc comment.
 * It can include tags like @param, @return, and @throws.
 */

Example:

/**
 * Calculates the sum of two numbers.
 *
 * @param a The first number.
 * @param b The second number.
 * @return The sum of the two numbers.
 */
public int add(int a, int b) {
    return a + b;
}

Best Practices for Using Comments

  1. Write Clear and Concise Comments: Use comments to explain the “why” behind the code, not the “what.” Avoid stating the obvious.
// Bad: Increment i by 1
i++;

// Good: Increment the loop counter
i++;
  1. Use Javadoc for Public APIs: Always use Javadoc comments for public methods, classes, and fields to generate documentation.
  2. Avoid Over-Commenting: Too many comments can clutter the code. Focus on explaining complex logic or important details.
  3. Keep Comments Up-to-Date: Ensure that comments are updated whenever the code changes to avoid confusion.
  4. Use Comments for Debugging: Temporarily comment out code during debugging, but remember to remove or uncomment it afterward.

Common Mistakes to Avoid

  1. Outdated Comments: Leaving outdated comments can mislead other developers.
// This method calculates the difference (outdated)
public int add(int a, int b) {
    return a + b;
}
  1. Over-Commenting: Writing comments for every line of code can make the code harder to read.
int i = 0; // Initialize i to 0
i++; // Increment i by 1
  1. Ignoring Javadoc: Failing to document public APIs can make it difficult for others to use our code.

Conclusion

Comments are a powerful tool for making our Java code more readable, maintainable, and well-documented. By using single-line, multi-line, and Javadoc comments effectively, we can improve the quality of our code and make it easier for others to understand. Whether we’re writing a small script or a large application, mastering the use of comments is essential for becoming a proficient Java developer.

Frequently Asked Questions

What is the purpose of comments in Java?

Comments in Java are used to provide explanations and documentation within the code. They are ignored by the compiler and are meant for developers to understand the code.

Can I nest multi-line comments in Java?

No, Java does not support nesting of multi-line comments. Attempting to nest them will result in a compilation error.

Is it a good practice to comment every line of code?

While comments are helpful for clarity, excessive comments can clutter the code. It’s generally best to use comments judiciously to explain complex logic or provide context.

Do comments affect the performance or size of the compiled Java code?

No, comments have no impact on the performance or size of the compiled code. They are entirely removed during the compilation process and do not affect the program’s execution.

Related Posts:

  • Control Statements in Java
  • What is Java? Exploring Its Key Features, Benefits,…
  • User Defined Exception in Java
  • MySQL Commands for Developers
  • What Is Java Swing? A Complete Guide to Java’s GUI Toolkit
  • Identifiers in Java
Tags:javalanguage-fundamentals
Was this article helpful?
← Previous ArticleArrays of Primitive Types in Java
Next Article →Garbage Collection in Java

Recent Posts

  • How to implement Passwordless Authentication in Spring Boot: A Step-by-Step Guide
  • How to Use AWS CloudFront Signed URLs in Spring Boot?
  • How to Fix SSH Agent Forwarding on macOS: The Ultimate Guide for Developers
  • How to Read AWS Secrets Manager in Spring Boot (Step-by-Step)
  • How to Fix “Public Key Retrieval is not allowed” MySQL JDBC Error
CoderSathi

Your go-to resource for Java, Spring Boot, Microservices, AWS, and modern development tutorials.

Linkedin

Quick Links

  • About
  • Contact

Popular Topics

  • Java
  • Spring Boot
  • AWS
  • DevOps
  • MongoDB
  • Linux
  • Git
  • How to
© 2026 CoderSathi. All rights reserved. Privacy Policy · Sitemap