Skip to main content

Java Comments

Java comments are non-executable statements used to explain code. They are ignored by the compiler and help improve code readability and maintenance.

Types of Java Comments

1. Single-Line Comment

  • Starts with //
  • Used for short explanations
// This is a single-line comment
System.out.println("Hello");

2. Multi-Line Comment

  • Starts with /* and ends with /
  • Used for longer explanations
/*
This is a multi-line comment
It can span multiple lines
*/
System.out.println("Hello");

3. Documentation Comment (Javadoc)

  • Starts with /** and ends with /
  • Used to generate documentation
/**
* This method prints a message
* @author Midhun
*/
publicvoidprintMessage() {
System.out.println("Hello");
}

Key Points

  • Comments are ignored by the Java compiler
  • Improve readability and maintainability
  • Useful for:
    • Explaining logic
    • Debugging (temporarily disable code)
    • Documentation

Best Practices

  • Keep comments clear and meaningful
  • Avoid obvious comments like:
    intx=10;// assign 10 to x ❌
  • Use Javadoc for:
    • Public classes
    • Methods and APIs

Quick Summary

  • // → Single-line
  • /* */ → Multi-line
  • /** */ → Javadoc