Last Updated : 20 Nov, 2024
Try it on GfG Practice
Comments are a very important part of any programming language that enhances the readability of the codes. While compiling a code, comments are always ignored by the Java compiler. So you can also test alternative code by commenting on the other code or you can say comments can be used for debugging purposes.
In Java, there are 3 types of comments - Single Line, Multi-Line, and Documentation comments.
Example:
// This is single line comment1. Single-Line CommentsSystem.out.println("Single - Line Comments");
/*This is a multi
line comment*/System.out.println("Multi-Line Comments");
/**
* Documentation Comments
* Documentation Comments
*/System.out.println("Documentation Comments");
A beginner-level programmer uses mostly single-line comments for describing the code functionality. It's the easiest typed comments.
Syntax:
// Comments here( Text in this line only is considered as comment )
Example:
Java
// Java program to show single line comments
class GFG
{
public static void main(String args[])
{
// Single line comment here
System.out.println("Single Line Comment Above");
}
}
Single Line Comment Above2. Multi-Line Comments
To describe a full method in a code or a complex snippet single line comments can be tedious to write since we have to give '//' at every line. So to overcome this multi-line comments can be used.
Syntax:
/*Comment starts
continues
continues
.
.
.
Comment ends*/
Example:
Java
// Java program to show multi line comments
class GFG
{
public static void main(String args[])
{
System.out.println("Multi Line Comments Below");
/* Comment line 1
Comment line 2
Comment line 3
*/
}
}
Multi Line Comments Below
We can also accomplish single line comments by using the above syntax as shown below:
/*Comment line 1*/3. Documentation Comments
This type of comment is used generally when writing code for a project/software package, since it helps to generate a documentation page for reference, which can be used for getting information about methods present, its parameters, etc.
Syntax:
/**Comment startJava
*
*tags are used in order to specify a parameter
*or method or heading
*HTML tags can also be used
*such as <h1>
*
*comment ends*/
// Java program to show the documentation comments
import java.io.*;
class GFG {
public static void main (String[] args) {
/**
comment line 1
comment line 2
*/
}
}
Available Tags to Use:
Example:
Java
// Java program to illustrate frequently used
// Comment tags
/**
* <h1>Find average of three numbers!</h1>
* The FindAvg program implements an application that
* simply calculates average of three integers and Prints
* the output on the screen.
*
* @author Pratik Agarwal
* @version 1.0
* @since 2017-02-18
*/
public class FindAvg
{
/**
* This method is used to find average of three integers.
* @param numA This is the first parameter to findAvg method
* @param numB This is the second parameter to findAvg method
* @param numC This is the second parameter to findAvg method
* @return int This returns average of numA, numB and numC.
*/
public int findAvg(int numA, int numB, int numC)
{
return (numA + numB + numC)/3;
}
/**
* This is the main method which makes use of findAvg method.
* @param args Unused.
* @return Nothing.
*/
public static void main(String args[])
{
FindAvg obj = new FindAvg();
int avg = obj.findAvg(10, 20, 30);
System.out.println("Average of 10, 20 and 30 is: " + avg);
}
}
Average of 10, 20 and 30 is: 20
For the above code documentation can be generated by using the tool 'javadoc':
Javadoc can be used by running the following command in the terminal.
javadoc FindAvg.java
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4