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 comment
System.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");
1. Single-Line 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");
}
}
OutputSingle Line Comment Above
2. 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
*/
}
}
OutputMulti 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 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*/
Java
// 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:
Tag | Description | Syntax |
---|
@author | Adds the author of a class. | @author name-text |
{@code} | Displays text in code font without interpreting the text as HTML markup or nested javadoc tags. | {@code text} |
{@docRoot} | Represents the relative path to the generated document’s root directory from any generated page. | {@docRoot} |
@deprecated | Adds a comment indicating that this API should no longer be used. | @deprecated deprecatedtext |
@exception | Adds a Throws subheading to the generated documentation, with the classname and description text. | @exception class-name description |
{@inheritDoc} | Inherits a comment from the nearest inheritable class or implementable interface. | Inherits a comment from the immediate surperclass. |
{@link} | Inserts an in-line link with the visible text label that points to the documentation for the specified package, class, or member name of a referenced class. | {@link package.class#member label} |
{@linkplain} | Identical to {@link}, except the link’s label is displayed in plain text than code font. | {@linkplain package.class#member label} |
@param | Adds a parameter with the specified parameter-name followed by the specified description to the “Parameters” section. | @param parameter-name description |
@return | Adds a “Returns” section with the description text. | @return description |
@see | Adds a “See Also” heading with a link or text entry that points to reference. | @see reference |
@serial | Used in the doc comment for a default serializable field. | @serial field-description | include | exclude |
@serialData | Documents the data written by the writeObject( ) or writeExternal( ) methods. | @serialData data-description |
@serialField | Documents an ObjectStreamField component. | @serialField field-name field-type field-description |
@since | Adds a “Since” heading with the specified since-text to the generated documentation. | @since release |
@throws | The @throws and @exception tags are synonyms. | @throws class-name description |
{@value} | When {@value} is used in the doc comment of a static field, it displays the value of that constant. | {@value package.class#field} |
@version | Adds a “Version” subheading with the specified version-text to the generated docs when the -version option is used. | @version version-text |
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);
}
}
OutputAverage 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
Similar Reads
Java Features
Java is a high-level, object-oriented programming language. It is known for its platform independence, reliability, and security. Java Programming language follows the "Write Once, Run Anywhere" principle. It provides various features like portability, robustness, simplicity, multithreading, and hig
7 min read
Java Identifiers
An identifier in Java is the name given to Variables, Classes, Methods, Packages, Interfaces, etc. These are the unique names used to identify programming elements. Every Java Variable must be identified with a unique name. Example: public class Test{ public static void main(String[] args) { int a =
2 min read
Java -verbose Command
Java Development Kit (JDK) is the Software Development Kit that contains all the tools and files required for compiling, developing, and running Java programs. JDK has the Java Compiler Program known as Javac which compiles the Java source files to the bytecode class files i.e. ( .java to .class). J
4 min read
What is Core Java?
Java is a programming language that is class-based and object-oriented. It is designed to be general-purpose and aims to have fewer implementation dependencies, and serves as a computing platform for application development. However, many new to the software industry often ask what Core Java is. Wha
7 min read
Byte Code in Java
Byte Code Byte Code can be defined as an intermediate code generated by the compiler after the compilation of source code(JAVA Program). This intermediate code makes Java a platform-independent language. How is Byte Code generated? Compiler converts the source code or the Java program into the Byte
2 min read
Java Cheat Sheet
Java is a programming language and platform that has been widely used since its development by James Gosling in 1991. It follows the Object-oriented Programming concept and can run programs written on any OS platform. Java is a high-level, object-oriented, secure, robust, platform-independent, multi
15+ min read
Java Applet Class
Java Applet is a special type of small Java program embedded in the webpage to generate dynamic content. The specialty of the Java applet is it runs inside the browser and works on the Client side (User interface side). Note:Applets, which are small programs that can be run inside a web page, are no
4 min read
Coding Guidelines in Java
Java is one of the most popular and widely used programming languages and platforms. A platform is an environment that helps to develop and run programs written in any programming language. Java is fast, reliable, and secure. From desktop to web applications, scientific supercomputers to gaming cons
7 min read
Java Applet Basics
Java Applets was once a very popular feature of web applications. Java Applets were small programs written in Java that ran inside a web browser. Learning about Applet helps us understand how Java has evolved and how it handles graphics. Note: java.applet package has been deprecated in Java 9 and la
9 min read
Java URL Class
URL class in Java is a part of java.net package that makes it easy to work with Uniform Resource Locators (URLs). URL is simply a string of text that identifies all the resources on the internet, telling us the address of the resource, how to communicate with it, and retrieve something from it. This
4 min read