Java comments

Introduction

Comments are used to document the program in order to understand the code or logic. It is ignored by the compiler and simply used to describe program operations. Java supports three types of comments, which are described in the following sections.

Single-line comments

A single line comment is used to describe code on a single line.

Syntax:

// Comment text

Example:

//Defining the main method 
public static void main(String args[]){ 
}

Single line comments can be used in the following two ways,

  • Start of line commenting, which is placed before the code.
  • End of line commenting, which is placed at the end of the code on the same line.

Example:

//Defining the main method 
public static void main(String args[]){ 
} // End of the main method

Multi-line comments

A multi-line comment is written on multiple lines. It starts with /* and ends with */. The compiler ignores everything between the two characters.

Example:

/* 
* The main method is the point of 
* execution of a Java program 
*/ 
public static void main(String args[]){ 
}

Javadoc comments

The Javadoc comment style is usually used to document classes, methods, etc. of a program. Javadoc comments begin with /** and end with */.

A comment can take several lines and anything between characters is ignored by the compiler. The javadoc command is used to generate Javadoc comments.

Example:

/** 
* This is the main method which is the point of execution. 
* @param args Not used. 
* @return Nothing. 
*/ 
public static void main(String args[]){ 
}

Here, @param, @return are examples of built-in tags recognized by Javadoc comments.

Leave a Comment