Site icon TestingDocs.com

Types of Java Comments

Introduction

Java comments are useful explanations to make the program understandable to others. Comments are ignored by the compiler and interpreter but are useful to programmers and other humans. Comments are not executed. The Java programming language supports three kinds of comments:

Single Line comment

Single line comment as the name indicates can only have a single line. Syntax for a single line comment:

Syntax

// comment text short and crispy

 

 

A single line comment starts with //

Make sure the comment is short and crispy to convey the message. For example,

//declare variables

We use single line comment to increase the readability of the Java program. For example, when the program has multiple nested blocks, we use the comments to denote where the code block ends.

 

package com.testingdocs.demo;

public class SampleProgram {
	//Program Entry point
	public static void main(String args[]) {
		String output="Learn Java - www.TestingDocs.com";
		System.out.println(output);
	} // end main
} // end class

Notice how we have used comments to denote the end of code blocks to avoid confusion. This would be extremely useful when we use nested control, loop structures.

Multi-line comment

A multi-line comment can span across multiple lines. We can use multi-line comments for lengthy comments like program headers, license terms, code explanations etc.

Syntax

/* multi-line comment text */

Example

/*
This is a multi-line comment. This comment
can have multiple lines.

To increase readability, * is added for all the lines
*/

 

IDE Code editors generally change the color of the comments to increase readability of the Java program.

Documentation comment

Java documentation comments add to the HTML documentation created by the javadoc tool.

Syntax

/** java documentation comment */

Example

/**
This indicates a documentation comment. The compiler ignores this kind of comment too. The javadoc tool uses documentation comments when preparing automatically generated documentation.
*/

 

Notice that two ** in the first line. More information on documentation comment:

https://www.testingdocs.com/java-documentation-comments/

 

That’s it. We have successfully gone through different types of Java comments.

Java Tutorials

Java Tutorial on this website:

https://www.testingdocs.com/java-tutorial/

For more information on Java, visit the official website :

https://www.oracle.com/java/

Exit mobile version