In Java, comments are statements ignored by the compiler, used to make the code more readable and to provide documentation.
Comments improve code readability and help developers understand logic easily.
Java supports three types of comments: Single-line, Multi-line, and Documentation (Javadoc) comments.
Documentation comments can be used to generate API docs using Javadoc tool.
Used for small explanations. Starts with //
.
// This is a single-line comment
public class Demo {
public static void main(String[] args) {
System.out.println("Hello World"); // Prints Hello World
}
}
Used when you need to write explanations spanning multiple lines. Enclosed in /* ... */
.
/*
This is a multi-line comment
It can span across multiple lines
*/
public class MultiLineDemo {
public static void main(String[] args) {
System.out.println("Example with multi-line comment");
}
}
Special type of comment used to generate documentation. Enclosed in /** ... */
and processed by Javadoc
tool.
/**
* This class demonstrates documentation comment.
* @author ProwessApps
* @version 1.0
*/
public class DocDemo {
/**
* This method adds two numbers.
* @param a first number
* @param b second number
* @return sum of a and b
*/
public int add(int a, int b) {
return a + b;
}
}
javadoc
.Topic: Comments | Language: Java
/** Example comment */
System.out.println(10 /* comment */ + 20);