Comments are notes written inside a program to explain the code. They help programmers understand, maintain, and improve their programs. Java comments are ignored by the compiler as executable instructions.
Comments are explanatory notes added to source code. They describe what a program or a particular statement does.
public class Hello {
public static void main(String[] args) {
// Display a welcome message
System.out.println("Welcome to Java");
}
}
Output:
Welcome to Java
The comment explains the purpose of the print statement. It does not appear in the program output.
Java supports three commonly used comment forms:
| Comment Type | Symbol | Purpose |
|---|---|---|
| Single-line | // |
Explains a single line or short statement. |
| Multi-line | /* ... */ |
Explains code across multiple lines. |
| Documentation | /** ... */ |
Documents program elements for tools such as Javadoc. |
A single-line comment begins with two forward slashes
(//). The comment continues until the end
of that line.
public class Example {
public static void main(String[] args) {
// Store a number
int number = 10;
// Display the number
System.out.println(number);
}
}
Output:
10
Use single-line comments to explain a variable, statement, or small section of code.
Multi-line comments begin with /* and end
with */. They can cover several lines.
public class Example {
public static void main(String[] args) {
/*
This program displays
a welcome message.
*/
System.out.println("Welcome!");
}
}
Output:
Welcome!
Multi-line comments are useful when an explanation requires more than one line.
Documentation comments begin with /**
and end with */. They can describe classes,
methods, constructors, and fields for Javadoc.
/**
* Represents a simple calculator.
*/
public class Calculator {
/**
* Adds two numbers.
*
* @param a first number
* @param b second number
* @return the sum of both numbers
*/
public int add(int a, int b) {
return a + b;
}
}
Javadoc can use these comments to generate HTML documentation for the class and its method.
You can also place a short comment after a statement on the same line.
int age = 20; // Student age
double price = 99.50; // Product price
boolean isActive = true; // Account status
System.out.println(age);
System.out.println(price);
System.out.println(isActive);
Output:
20
99.5
true
Keep inline comments short and useful so that they do not make code difficult to read.
Comments can explain the purpose of variables, especially when their meaning is not immediately clear.
public class Student {
public static void main(String[] args) {
// Student's full name
String name = "Rahul";
// Student's total marks
int marks = 85;
System.out.println(name);
System.out.println(marks);
}
}
Output:
Rahul
85
Choose meaningful variable names first. Use comments when additional explanation is helpful.
Comments can explain what a method does, what information it receives, and what it returns.
public class Calculator {
// Returns the sum of two integers
public static int add(int a, int b) {
return a + b;
}
public static void main(String[] args) {
System.out.println(add(10, 20));
}
}
Output:
30
For public APIs, documentation comments are often more useful than a simple explanatory comment.
Adding or removing an ordinary comment does not change the program's behavior, provided the actual code stays the same.
public class Demo {
public static void main(String[] args) {
// This comment is ignored as an instruction
System.out.println("Java");
/*
This comment does not
produce output either.
*/
}
}
Output:
Java
Comments are intended for people and documentation tools, not as executable program statements.
During testing, a programmer may temporarily disable a statement by commenting it out.
public class Demo {
public static void main(String[] args) {
System.out.println("First message");
// System.out.println("Second message");
System.out.println("Third message");
}
}
Output:
First message
Third message
The second statement does not execute because it is part of a comment.
// comments continue to the end of the line./* ... */ comments can span multiple lines./** ... */ comments are suitable for Javadoc documentation.Example of a block comment:
/*
This is valid.
It spans multiple lines.
*/
Incorrect nested block comment:
/*
Outer comment
/* Inner comment */
*/
The inner closing marker ends the block comment earlier than expected, which can cause compilation errors.
Bad example:
// Add 1 to x
x = x + 1;
This comment repeats what the code already clearly says.
More useful example:
// Move to the next page of results
pageNumber = pageNumber + 1;
A good comment explains the purpose or reason behind the operation rather than merely repeating the code.
Write a Java program using single-line, multi-line, and documentation comments.
/**
* Demonstrates Java comments.
*/
public class CommentDemo {
public static void main(String[] args) {
// Store the student's name
String name = "Rahul";
/*
Display the student's
name on the screen.
*/
System.out.println(name);
}
}
Output:
Rahul
Summary:
//./* ... */./** ... */.1. Which symbol is used for a single-line comment in Java?
2. Which comment type is used for Javadoc documentation?
3. Do ordinary Java comments execute as program instructions?