⬅ Previous Topic
Java Hello World ProgramNext Topic ⮕
Java Variables⬅ Previous Topic
Java Hello World ProgramNext Topic ⮕
Java VariablesEvery great codebase tells a story—not just through what it does, but how clearly it's explained. Comments in Java are a crucial part of that narrative. They don't affect the program execution, but they enrich the code for the humans who read it, maintain it, and debug it.
Whether you're working on your own project or collaborating with a team, using comments properly ensures that your intentions are clearly communicated.
Java supports three main types of comments:
These are used for short explanations or notes.
// This is a single-line comment
System.out.println("Hello, World!"); // Print statement
Hello, World!
Explanation: The lines starting with //
are ignored by the compiler. Only the System.out.println()
line executes and prints the output.
Used when you want to explain something in more than one line. Helpful for temporarily disabling blocks of code during testing or debugging.
/* This is a multi-line comment.
It can span several lines.
Useful for detailed notes or explanations. */
System.out.println("Multi-line comment demo");
Multi-line comment demo
Note: Everything between /*
and */
is ignored by the compiler.
Specially formatted comments used to generate official documentation. These comments are placed just before class, method, or field declarations.
/**
* This class demonstrates JavaDoc comments.
* @author Mallikarjuna
* @version 1.0
*/
public class Example {
/**
* This method prints a greeting.
* @param name The name to greet
*/
public void greet(String name) {
System.out.println("Hello, " + name);
}
}
Usage Tip: Use the javadoc
tool to generate HTML documentation from JavaDoc comments:
javadoc Example.java
Actually, you verify by what doesn’t happen:
Mastering comments is not just about syntax; it’s about thinking like a communicator. Clean, concise, and purposeful comments set apart a novice coder from a thoughtful developer. Use them wisely to make your code speak not just to the machine, but to your future self and fellow programmers.
// System.out.println("Commented out");
System.out.println("Active line");
⬅ Previous Topic
Java Hello World ProgramNext Topic ⮕
Java VariablesYou can support this website with a contribution of your choice.
When making a contribution, mention your name, and programguru.org in the message. Your name shall be displayed in the sponsors list.