Java Comments | Types of Comments in Java with Examples

Here, we have discussed about the Java comments. Here, we have learnt what is Java comments, types and uses of Java comments. Java comments are....

In this article we will learn about the Java comments. 

Java Comments | Types of Comments in Java with Examples

Here, we will discuss what is Java comments, types and uses of Java comments.

What is Java Comments?

Java comments are those portions of program which are ignored and not executed by the Java interpreter and compiler. Java comments are mainly used to make the program more understanding and readable. Comments in Java explains or give the information about method, class, statement or variable.

Types of Java comments

There are three types of Java comments which are explained as follows;

Java Single line comments 

The single line comments are those comments in which only a single line of code is used for commenting the statements. Most of the beginner programmers uses the single line comments to explain the functionality of code. In Java, // is used in the starting of single line comments.

The following is the syntax of single line comments;

//This is single line comment in Java 

Java SingleLine Comment Example;

// Java "Hello World" program 

class Main {

    public static void main(String[] args) {     

    {

        System.out.println("Hello World");

    }

}

Output

Hello World

Java Multiline comments

The multi line comments are those comments which are used for commenting multiple line of code or statements. In order to explain the complex code snippet, we uses the multi line comments. The multi line comments starts with /* and ends with */.

/* This is an example of multi-line comment */

Java Multi-line Comment Example;

 /* Java "Hello World!" in multi-line comment */

class HelloWorld {

public static void main(String[] args) {

{

System.out.println("Hello World!");

}

}

Output

Hello World!

Java Documentation comments

In Java, documentation comments are those comments which are used to write code of large project or software packages. Documentation comments are mainly used to create documentation APIs which gives the information or explanation of methods, parameters, arguments, classes, etc. The documentation comments starts with /** and ends with */.

/**Comment start

*

*<h1>Welcome to Answersjet.com</h1>

*Free Java Basic course

*Tutorials 

*

*Thanks for visiting Answersjet

*comment ends*/

Why use Java comments?

Now, let's discuss why we should use Java comments or what are the uses of Java comments;

• Java comments are used to make the program more understanding to the person.

• It also helps in making the program more readable.

• Java comments also helps in detecting errors easily.

• Java comments also provides the information and explanation of class, code, variable, statement, program, methods, etc.

• With the use of Java comments, the code becomes easy to maintain.

Conclusion

Above we have discussed about the Java comments. Here, we have learnt what is Java comments, types and uses of Java comments. Java comments are those portions of program which are ignored and not executed by the Java interpreter and compiler. There are three types of Java comments i.e., Single line, multi line and documentation comments.