JAVA Tutorial



COMMENTS IN JAVA


Comments in Java

In Java, comments are used to explain code and make it more readable. They are ignored by the compiler and do not affect the program's output. You can use comments to describe what your code is doing, why you're doing it, or to temporarily disable code.

Why Use Comments?

  • Improve code readability
  • Explain logic to yourself or others
  • Help in debugging by disabling certain lines

Types of Comments in Java

  • Single-line Comment โ†’ Begins with //
  • Multi-line Comment โ†’ Enclosed between /* and */
  • Documentation Comment โ†’ Starts with /** and is used to generate docs using javadoc

๐Ÿงช Examples

// This is a single-line comment
System.out.println("Hello, World!"); // Print statement

/*
This is a multi-line comment
It can span across multiple lines
*/

public class HelloWorld {
  /**
   * This is a documentation comment
   * @author Technorank
   * @version 1.0
   */
  public static void main(String[] args) {
    System.out.println("Java Comments Example");
  }
}
  

๐Ÿ“š Explanation

  • // is ideal for short, quick notes.
  • /* ... */ is useful for explaining sections or disabling multiple lines.
  • /** ... */ is mainly for generating external documentation.

Tip: Use comments wisely. Too many can clutter your code. Aim for clarity, not repetition.

๐Ÿ“ฑ Responsive Tip

On mobile or small screens, it's better to use collapsible examples or tabs if showing multiple code blocks. Also, ensure code blocks scroll horizontally to avoid layout breaking.

โœ… Conclusion

Java comments are powerful tools for writing maintainable and collaborative code. Whether you're working alone or in a team, proper use of comments ensures your code remains understandable and clean.

Practice Task: Try writing a Java program with all three types of comments. Add comments to explain each section of your code.


๐ŸŒŸ Enjoyed Learning with Us?

Help others discover Technorank Learning by sharing your honest experience.
Your support inspires us to keep building!

Leave a Google Review