
Comments in Java
In the previous tutorial, you wrote your first Java program and got familiar with its basic structure. Now, let’s learn about comments in Java and how they help developers write clean and understandable code.
What Are Comments in Java?
Comments in Java are non-executable statements written in the source code that explain, describe, or document the code.
- Comments are ignored by the Java compiler.
- They do not affect program execution.
- They exist only to help developers understand the code better.
class HelloWorld {
public static void main(String[] args) {
// This program prints a message
System.out.println("Hello,World!");
}
}
Types of Comments in Java
Java supports three types of comments:
- Single-line comments.
- Multi-line comments.
- Documentation (Javadoc) comments.
Let’s explore each one in detail.
1. Single-Line Comments (//)
Single-line comments in Java start and end on the same line. To write a single-line comment in Java, use the // symbol. Any text written after // is ignored by the compiler.
Example
// This program prints a message
System.out.println("Hello, World!");
You can also write a single-line comment after a statement:
int userAge = 25; // storing user's age
Or you can also write a single-line comment to temporarily disable a line during debugging:
//int userAge = 30;
int userAge = 25;
When to Use Single-Line Comments
- To explain a single line of code.
- To add short notes.
- To temporarily disable a line during debugging.
2. Multi-Line Comments (/* */)
Multi-line comments in Java are used when we want to write comments across multiple lines. To write a multi-line comment, we use the /* … */ symbols.
Example
/*
This program calculates the sum
of two numbers entered by the user
*/
int sum = a + b;
When to Use Multi-Line Comments
- To explain complex logic.
- To write detailed explanations.
- To comment out multiple lines of code at once.
3. Documentation Comments (Javadoc) (/** */)
Documentation comments are a special type of multi-line comments used to generate documentation automatically.
They start with /** and end with */.
Example
/**
* This class performs basic arithmetic operations
* @author Harsh
* @version 1.0
*/
public class Calculator {
}
Common Javadoc Tags
- @param: describes a method parameter
- @return: describes return value
- @author: author of the code
- @version: version information
When to Use Documentation Comments
- To Document Class or Methods.
Method Example
/**
* Adds two integers
* @param a first number
* @param b second number
* @return sum of a and b
*/
public int add(int a, int b) {
return a + b;
}
How Java Compiler Handles Comments
- The Java compiler completely ignores comments.
- Comments are not included in bytecode.
- Comments do not affect performance.
- They are removed during compilation.
Best Practices for Writing Comments in Java
Follow these best practices to write effective comments:
1. Write meaningful comments: Explain why something is done, not what is obvious.
int x = 10; // assigning 10 to x (This is not necessary)
2. Keep comments updated: Outdated comments can confuse developers.
3. Use comments to explain complex logic: Especially helpful in algorithms and business logic.
/* This class handles complex order pricing logic
* used in an e-commerce application.
* Business Goal:
* Apply 10% discount if order value is above ₹2,000
* Maximum discount allowed is ₹300
*/
public class DiscountCalculator {
public static double calculateDiscount(double orderAmount) {
// Default discount is zero
double discount = 0;
// Apply discount only if order crosses ₹2000
if (orderAmount > 2000) {
// Calculate 10% discount
discount = orderAmount * 0.10;
// Business rule: discount should not exceed ₹300
if (discount > 300) {
discount = 300;
}
}
// Return final discount amount
return discount;
}
}
4. Use proper formatting: Well-formatted comments improve readability.
/*
This program calculates the sum
of two numbers entered by the user
*/
int a = 10;
int b = 20;
int sum = a + b;
Common Mistakes While Using Comments
Avoid these common errors:
- Over-commenting simple code.
- Commenting on bad code instead of fixing it.
- Writing misleading or outdated comments.
- Using comments instead of meaningful variable names.
Good Comments vs Bad Comments
Poor Commenting:
// this method does something
public void process() {
// code here
}
Good Commenting:
// Calculates total price after applying discount
public double calculateFinalPrice(double price, double discount) {
return price - discount;
}
You should avoid comments when:
- Code is self-explanatory.
- Proper naming makes comments unnecessary.
- Comments repeat what the code already says.
Comments in Java Interview Questions
Here are some common interview questions:
- Are comments executed in Java?
- What are the types of comments in Java?
- What is Javadoc?
- Can comments affect performance?
- Can we write comments inside methods?
Summary
Let’s quickly recap:
- Comments are ignored by the Java compiler.
- Java supports three types of comments:
- Single-line.
- Multi-line.
- Documentation (Javadoc).
- Comments improve readability and maintainability.
- Use comments wisely and avoid over-commenting.
What’s Next?
Java Tutorials
Stay Connected
Follow on Instagram for Java tips, coding reels, and much more. Subscribe to VairagiCodes YouTube Channel for learning the art of programming.









