TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

Java Comments

Java comments, single line java comment, multi line java comment, documentation comment for creating java documentation with examples of fibonacci series, armstrong number, prime number, palindrome number etc.

<< Back to JAVA

Java Comments

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

Why do we use comments in a code?

  • Comments are used to make the program more readable by adding the details of the code.
  • It makes easy to maintain the code and to find the errors easily.
  • The comments can be used to provide information or explanation about the variable, method, class, or any statement.
  • It can also be used to prevent the execution of program code while testing the alternative code.

Types of Java Comments

There are three types of comments in Java.

  1. Single Line Comment
  2. Multi Line Comment
  3. Documentation Comment
Java Types of Comments

1) Java Single Line Comment

The single-line comment is used to comment only one line of the code. It is the widely used and easiest way of commenting the statements.

Single line comments starts with two forward slashes (//). Any text in front of // is not executed by Java.

Syntax:

//This is single line comment

Let's use single line comment in a Java program.

CommentExample1.java

public class CommentExample1 {  
public static void main(String[] args) {  
int i=10; // i is a variable with value 10
System.out.println(i);  //printing the variable i
}  
}  

Output:

10

2) Java Multi Line Comment

The multi-line comment is used to comment multiple lines of code. It can be used to explain a complex code snippet or to comment multiple lines of code at a time (as it will be difficult to use single-line comments there).

Multi-line comments are placed between /* and */. Any text between /* and */ is not executed by Java.

Syntax:

/* 
This  
is  
multi line  
comment 
*/  

Let's use multi-line comment in a Java program.

CommentExample2.java

public class CommentExample2 {  
public static void main(String[] args) {  
/* Let's declare and 
 print variable in java. */  
  int i=10;  
    System.out.println(i);  
/* float j = 5.9;
    float k = 4.4;
    System.out.println( j + k ); */  
}  
}  

Output:

10

Note: Usually // is used for short comments and /* */ is used for longer comments.

3) Java Documentation Comment

Documentation comments are usually used to write large programs for a project or software application as it helps to create documentation API. These APIs are needed for reference, i.e., which classes, methods, arguments, etc., are used in the code.

To create documentation API, we need to use the javadoc tool. The documentation comments are placed between /** and */.

Syntax:

/** 
*
*We can use various tags to depict the parameter
*or heading or author name
*We can also use HTML tags  
*
*/  

javadoc tags

Some of the commonly used tags in documentation comments:

Tag Syntax Description
{@docRoot} {@docRoot} to depict relative path to root directory of generated document from any page.
@author @author name - text To add the author of the class.
@code {@code text} To show the text in code font without interpreting it as html markup or nested javadoc tag.
@version @version version-text To specify "Version" subheading and version-text when -version option is used.
@since @since release To add "Since" heading with since text to generated documentation.
@param @param parameter-name description To add a parameter with given name and description to 'Parameters' section.
@return @return description Required for every method that returns something (except void)

Let's use the Javadoc tag in a Java program.

Calculate.java

import java.io.*;

/**
 * <h2> Calculation of numbers </h2>
 * This program implements an application
 * to perform operation such as addition of numbers 
 * and print the result 
 * <p>
 * <b>Note:</b> Comments make the code readable and 
 * easy to understand.
 * 
 * @author Anurati 
 * @version 16.0
 * @since 2021-07-06
 */
 
 public class Calculate{
    /**
     * This method calculates the summation of two integers.
     * @param input1 This is the first parameter to sum() method
     * @param input2 This is the second parameter to the sum() method.
     * @return int This returns the addition of input1 and input2
     */
    public int sum(int input1, int input2){
        return input1 + input2;
    }
    /**
    * This is the main method uses of sum() method.
    * @param args Unused
    * @see IOException 
    */  
    public static void main(String[] args) {
        Calculate obj = new Calculate();
        int result = obj.sum(40, 20);

        System.out.println("Addition of numbers: " + result);
    }  
 } 

Compile it by javac tool:

Create Document

java comments

Create documentation API by javadoc tool:

java comments

Now, the HTML files are created for the Calculate class in the current directory, i.e., abcDemo. Open the HTML files, and we can see the explanation of Calculate class provided through the documentation comment.

Are Java comments executable?

Ans: As we know, Java comments are not executed by the compiler or interpreter, however, before the lexical transformation of code in compiler, contents of the code are encoded into ASCII in order to make the processing easy.

Test.java

public class Test{
	public static void main(String[] args) {
		//the below comment will be executed
// \u000d System.out.println("Java comment is executed!!");
	}
}

Output:

java comments

The above code generate the output because the compiler parses the Unicode character \u000d as a new line before the lexical transformation, and thus the code is transformed as shown below:

Test.java

public class Test{
	public static void main(String[] args) {
		//the below comment will be executed
//
System.out.println("Java comment is executed!!");
	}
}

Thus, the Unicode character shifts the print statement to next line and it is executed as a normal Java code.


Next TopicJava Programs




Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf