Java Comments

🔷 What are comments?

Comments are non-executing lines in a Java program. They are ignored by the compiler and used to explain code, improve readability, and help with documentation.


🔷 Types of Comments in Java:

Type Syntax Use Case
Single-line comment // comment here Brief explanations on a single line
Multi-line comment /* comment here */ Descriptions over multiple lines
Documentation /** comment here */ For generating HTML docs using Javadoc tool

📘 Example 1: Single-line comment

public class SingleLineComment {
public static void main(String[] args) {
// This prints a message
System.out.println("Hello Java");
}
}

📘 Example 2: Multi-line comment

public class MultiLineComment {
public static void main(String[] args) {
/* This is a multi-line comment.
It describes the following code. */

System.out.println("Welcome to Java!");
}
}

📘 Example 3: Documentation comment

/**
* This class prints a greeting message.
*
@author Chad
*/
public class DocumentationComment {
public static void main(String[] args) {
System.out.println("Hi, Students!");
}
}

You can generate documentation using:

javadoc DocumentationComment.java

📝 Why Use Comments?

  • ✅ Explain complex code

  • ✅ Make code easier to maintain

  • ✅ Help other developers understand your work

  • ✅ Create official documentation (Javadoc)

Related Posts