Skip to content

Comments In Java Programming Language

In this article, you will learn Different Types of Comments in Java Programming Language.

Comments are the informational statements that are part of a java program but are not involved in the execution process. They are completely ignored by the compiler and interpreter. However, they form an integral part of a java program.

Comments are used to increase readability and helps the programmers to understand the codes easily. They provide explanations about class and its members i.e, variables and methods. They are also used to hide a particular set of codes inside a java program for a specific period of time.

Also Read: What is a Java Virtual Machine?

TYPES OF COMMENTS IN JAVA

Basically, there are three types of comments supported by the java programming language.

  1. Single Line Comment
  2. Multi Line Comment
  3. Documentation Comment

These are described below in detail.

[1] SINGLE LINE COMMENT

A single line comment is used for short explanations like in variables, method declarations etc. Two forward slashes without white spaces are used to represent a single line comment. The syntax is shown below.

Syntax:

//single line comment goes here

Example:

class SingleLineComment {
public static void main(String[] args) {
String name = “Amit Negi” ; //Here, name is a variable whose value is “Amit Negi”
int salary = 50000 ; //Here, salary is a variable whose value is 50000
System.out.println(name) ;
System.out.println(salary) ;
}
}

Output:

Amit Negi
50000

[2] MULTI LINE COMMENT

A multi line comment is used where explanation or comment exceeds a line. These comments are declared between slash-star and star-slash without white spaces. The syntax is shown below.

Syntax:

/*  
multi
line 
comment
*/

Example:

class MultiLineComment {
public static void main(String[] args) {
/*
name and salary are variables
having values “Naveen Rawat”
and 2000 respectively.
*/
String name = “Naveen Rawat” ;
int salary = 2000 ;
System.out.println(name) ;
System.out.println(salary) ;
}
}

Output:

Naveen Rawat
2000

[3] DOCUMENTATION COMMENT

Java Documentation comments are used for long explanations such as details about author, version of class or interface, deprecated class and methods, return value, exceptions etc.

There comes a built-in tool Javadoc with the JDK which can be used to generate documentation comments in HTML format. Javadoc is designed and developed by Sun Microsystems.

The syntax of javadoc comment is quite similar to a multi line comment except there is an extra asterisk(/**) in the beginning tag.

Syntax:

/**
This is
the syntax of
a documentation
comment
*/

Javadoc Tags

The javadoc tool contains the following descriptive tags.

  1. @author
  2. {@code}
  3. @version
  4. @since
  5. @see
  6. @return
  7. @exception
  8. @param
  9. @throws
  10. {@inheritDoc}
  11. @deprecated
  12. {@value}
  13. {@code}
  14. {@literal}
  15. {@link}

Example1: Without using Tags

/**
* This is a simple example of Documentation Comments
* without using javadoc tags.
*/

class W3TrainingSchool {

public static void main(String[] args) {

/** This method is used to print
* Welcome To W3Training School in the output.
*/

System.out.println(“Welcome To W3Training School”) ;
}
}

Output 1:

Welcome To W3Training School

Example2: Using Javadoc Tags

/**
* <h1>Subtraction Example</h1>
* The following example gives the difference
* between two numbers and
* the output will be printed on the screen.
* This example also shows the use of javadoc tags.
*
* @author Naveen Rawat
* @version 1.0
*/

class SubtractNum {

/**
* The above method is used to subtract one number from another.
* @param n1 It represents the first parameter.
* @param n2 It represents the second parameter.
* @return int It will return the difference between n1 and n2.
*/
public int SubtractNum(int n1, int n2) {
return n1  n2 ;
}

/**
* SubtractNum method is utilized in the below main method to execute the codes.
* @param args Not used.
* @return Nothing will be returned.
* @exception IOException.
*/

public static void main(String args[]) throws IOException {
SubtractNum sub = new SubtractNum() ;
int difference = sub.SubtractNum(100, 50) ;

System.out.println(“Difference between n1 and n2 is :“ + difference) ;
}
}

Output 2:

Difference between n1 and n2 is : 50

We have provided you the description on Different Types of Comments in Java Programming Language. Hope you like this article. For more updates and related information, stay connected to our blogs on java programming language.

Facebook
Twitter
LinkedIn
Pinterest