Comments in Java
Program comments in Java programming are an important element of indicating or notating program source code created by a programmer. They allow Java beginners or expert users to add detailed explanations or annotations to their program source code. Comments help Java programmers or other users to easily understand or understand any program logic, condition, or expression in the program source code written by another user or for you in the future.

There are three main types of comments in the Java programming language.
- Single-line Java comments (//).
- Multi-line Java comments (/* */).
- Documentation Java comments (/** */).
So, let’s take a closer look at all types of comments in the Java language.
Single-line Java comments (//).
A single-line comment in the Java programming language starts with //. Here, any text expression or information written after // on a program source code line is treated as a comment and is ignored during Java program execution.
Single-line Java comments (//) syntax.
// Here, a single-line comment is given as an integer variable.
int p = 2; // Here, an inline comment is added to the integer p variable.
Example of single-line Java comments (//).
public class welcome msg {
public static void main(String[] args) {
// This is the entry point of any program, containing program logic expressions and user-defined conditions.
System.out.println(“Welcome to Java programming!”); // This prints the given line of text to the console screen.
}
Here in this example.
- In Java programs, a single-line comment // is the entry point of a program, indicating the location of that line of source code in the Java program.
- Here, the comment // defines the role of the System.out.println(“Welcome to Java programming!”); statement in the current program.
Multi-line Java Comments (/* */).
In Java programming, a multi-line comment can span multiple lines, and it starts with /* and ends with */. Here, the comment is created in multiple lines from start to end. Multi-line comments in Java programs are used to display comments on a block of program source code or to explain program logic, expression, or condition in detail.
Syntax of Multi-line Java Comments.
/* Here, you are given an example of a multi-line comment
which is divided into more than one multi-line */
Example of Multi-line Java Comments.
public class welcome msg {
public static void main(String[] args) {
/* The following text out line
prints a user-defined text message to the console screen. */
System.out.println(“Welcome to Java programming!”);
}
}
Here in this example.
- In this program above, the multi-line comment in the System.out.println statement defines or prints a user-defined text expression, and here the multiline comment is divided into two lines.
Documentation Comments (/** */) in Java.
Java programming has documentation comments, also known as Javadoc comments in Java. Documentation comments are used to create API documentation for user-defined Java classes and methods. Documentation comments start with /** and end with */. Documentation comments are commonly used to indicate the purpose of a declared class or method, as well as program variable parameters and return values.
Syntax of Java Documentation Comments.
/**
* Here is a Java program documentation comment.
* Documentation comments in Java can be used to create HTML documentation using the Javadoc tool.
*/
Example of Java Documentation Comments.
/**
* Here the welcome-msg class prints a welcome message to the console.
* Here is an example of using comments in a Java program.
*/
public class welcome-msg {
/**
* This is the entry point of the main method in any Java program.
* It prints the text message “Welcome to Java programming!” to the console screen.
*
* @param args Here you can define the command-line arguments for the program.
*/
public static void main(String[] args) {
System.out.println(“Welcome to Java programming!”);
}
}
Here in this example.
- In this example, the class-level documentation comment welcome-msg indicates the class.
- The main method in the Program class has its own documentation comment that defines its purpose in the program, and here @param is a tag defined to indicate any type of args defined in the command-line arguments.
Using comments in Java programs in an effective order.
Cleaning up complex code in large Java programs.
User-defined comments are used in any Java program to indicate or explain why and how specific program logic is applied in a particular order. Especially when the user-created Java program source code is complex or difficult to understand.
// Apply a loop to an array to calculate the total sum in a Java program
for (int p = 0; p < integer.length; p++) {
total += integer[p];
}
Marking sections of Java program source code.
Comments in Java programs can be used to mark sections of source code, making it easier to navigate large, complex program source code.
// This starts input handling in a Java program
String input = scanner.nextLine();
// This ends input handling in a Java program.
Commenting Java program source code during testing.
When debugging or testing large-volume, complex Java programs, you can temporarily disable specific portions of the program source code by adding comments to them.
// System.out.println(“Here, we have commented a line in the program for testing purposes.”);
Don’t overuse comments in Java programs.
Using excessive Java program comments complicates the logic, expressions, conditions, or method parameters in your program class. It’s best if Java programmers understand their own program source code and use comments only when necessary.
Instead of commenting to explain small lines of source code in Java programs, the program code written by the programmer should automatically be in a simple explain format.
int q = 9; // Instead of adding a comment here, display the name of the program parameter variable in the description.
Conclusion of Comments in Java.
- Comments in the Java programming language are used by programmers to explain program source code and make it simpler for other software developers or themselves.
- There are three main types of comments in the Java programming language.
- Add single-line comments (//) to express small information in a Java program.
- Add multi-line comments (/* */) to comment on large-volume Java program information or blocks of program source code.
- Add documentation comments (/** */) to create external API documentation using tools like Javadoc for Java program documentation comments.
