What are Comments in Java?
Comments are notes written inside a program to explain the code. They help developers understand the purpose of the program.
Java compiler ignores comments while executing the program.
Comments improve readability, maintainability, and understanding of code.
Types of Comments in Java
Java supports three types of comments:
- Single Line Comment
- Multi Line Comment
- Documentation Comment
Single Line Comment
Single line comments are used for short explanations. They start with double forward slashes //.
// This is single line comment
System.out.println("Java");
Output
Java
Everything written after // on the same line is ignored by Java compiler.
Single Line Comment Example
public class Main {
public static void main(String[] args) {
// Printing welcome message
System.out.println("Welcome");
}
}
Multi Line Comments
Multi line comments are used for large descriptions or explaining multiple lines of code.
They start with /* and end with */.
/* This is multi line comment. It can span multiple lines. Java compiler ignores it. */
Example
public class Test {
public static void main(String[] args) {
/*
This program prints
Hello Java message
*/
System.out.println("Hello Java");
}
}
Documentation Comments
Documentation comments are used to generate official Java documentation using the Javadoc tool.
They start with /** and end with */.
/** * This is documentation comment * used for generating documentation. */
Example
/**
* This class performs addition.
*/
public class Addition {
/**
* Main method
*/
public static void main(String[] args) {
System.out.println(10 + 20);
}
}
Why are Comments Important?
- Improve code readability
- Help beginners understand code
- Explain complex logic
- Useful for teamwork
- Improve code maintenance
- Help during debugging
Commenting Out Code
Developers often comment code temporarily for testing or debugging purposes.
public class Demo {
public static void main(String[] args) {
System.out.println("Line 1");
// System.out.println("Line 2");
System.out.println("Line 3");
}
}
Output
Line 1 Line 3
Rules for Java Comments
- Comments do not affect program execution
- Single line comments start with //
- Multi line comments start with /* and end with */
- Documentation comments start with /**
- Nested multi line comments are not allowed
Nested Comments are Invalid
Java does not support nested multi line comments.
/* This is invalid /* Nested comment */ */
Nested multi line comments cause compilation errors.
Best Practices for Writing Comments
- Write meaningful comments
- Keep comments short and clear
- Do not write unnecessary comments
- Explain logic, not obvious code
- Update comments when code changes
Bad vs Good Comments
Bad Comment
// Increment i i++;
This comment is unnecessary because the code is already obvious.
Good Comment
// Retry connection after network failure retryConnection();
This comment explains the purpose of the code.
Complete Example
/**
* Java Comments Example
*/
public class CommentsDemo {
public static void main(String[] args) {
// Variable declaration
int age = 20;
/*
Printing age value
on screen
*/
System.out.println(age);
}
}
Output
20
Common Mistakes
- Forgetting to close multi line comments
- Writing outdated comments
- Using too many unnecessary comments
- Using nested comments
- Writing confusing comments
Conclusion
Comments are extremely useful in Java programming. They improve readability, teamwork, debugging, and maintainability of code.
Every developer should learn how to write proper comments to create clean and professional programs.