Comments in Java

Comments in Java, writing comments in programs, single-line comments, multi-line comments, and Java documentation comments.

Programming Comments are English words used for code readability and code documentation.

Java Comments

1. Purpose of Comments

The Java comments are the statements that are not executed by the compiler and interpreter.

The comments can be used to provide information or explanation about the variable, method, class, or any statement.

The comments can be used to hide program code.

Note: The primary purpose of comments is to make the code understandable and the secondary purpose of comments is to make the code prevent execution.

2. Comments Syntax

In Java there are three types of comments:

a. Single – line comments.
b. Multiline comments.
c. Documentation comments.

a. Use // for Single line comment

b. Use /*………….
…………………
…………………
………………*/ for Multiple line comment

Or
Comment Multiple Lines

(Eclipse IDE)
Select Statements/Steps
Source Menu -> Add Block Comment

Uncomment

(Eclipse IDE)
Select Comment block
Source Menu -> Remove Block Comment

c. Documentation Comments

This type of comments are used generally when writing code for a project/software package since it helps to generate a documentation page for reference.

Syntax:

/**Comment start
*
*tags are used in order to specify a parameter
*or method or heading
*HTML tags can also be used
*such as <h1>
*
*comment ends*/

3. Example:

public class Sample2 {

public static void main(String[] args) {
int a, b; //Variables Declaration
a =10; b=20;
int c = a+b;
//Display the Addition value of a and b
System.out.println(“Addition of a, b is: “+c);
System.out.println(“”);

/*if (a > b) {
System.out.println(“A is a Big Number”);
}
else
{
System.out.println(“B is a Big Number”) ;
}*/

}
}

4. Usage of Comments in Test Automation

a) To Write Test case header

b) To Write Method header

c) To Explain Complex Logic


Java Programming Language Syllabus

Java Step by Step Videos

Follow me on social media: