Why Comments?
Comments are used to explain Java code, and to make it more readable. They are ignored by the compiler.
1. Single-line Comments
Starts with two forward slashes
//.
// This is a comment
System.out.println("Hello"); // This is also a comment
2. Multi-line Comments
Starts with
/* and ends with
*/.
/* The code below will print the words Hello World
to the screen, and it is amazing */
System.out.println("Hello World");
3. Documentation Comments (Javadoc)
Starts with
/** and ends with
*/. Used to generate API documentation.
/**
* This method calculates the sum of two integers.
* @param a First integer
* @param b Second integer
* @return Sum of a and b
*/
public int sum(int a, int b) {
return a + b;
}