Java Comments
Comments in Java are used to describe code, improve readability, and make
programs easier to understand. They are ignored by the compiler, meaning
they do not affect program execution.
Comments are especially useful for:
- Explaining complex logic
- Leaving notes for other developers
- Temporarily disabling code during testing
Single-Line Comments
A single-line comment begins with //. Everything written after // on the
same line is ignored by Java.
Example: Comment Above a Statement
// Display a welcome message
System.out.println("Welcome to the application");
Example: Comment After a Statement
System.out.println("Program started"); // This confirms the program has
begun In both cases, the comment is not executed.
Multi-Line Comments
Multi-line comments begin with /* and end with */. All text written between
these symbols is ignored by the compiler.
This type of comment is often used for longer explanations.
Example
/*
The following statement calculates and prints the total score
of a student.
*/
System.out.println(85 + 15);
When to Use Each Type
- Use // for short, quick explanations
- Use /* */ for detailed descriptions or documentation
Both types serve the same purpose: improving code clarity without
affecting execution.