Java Comments

We’ll cover the following

  • Introduction
  • Type Java Comments
    • End-of-line comments
    • Traditional comments, and
    • Documentation comment

Introduction

A comment is the text that provides explanations of your code.

Like most other programming languages, Java lets you enter a remark into a program’s source file. The contents of a comment are ignored by the compiler. Instead, a comment describes or explains the operation of the program to anyone who is reading its source code.

Type Java Comments

Java has three basic types of comments:

  • End-of-line comments,
  • Traditional comments, and
  • Documentation comment

End-of -line comments

An end-of-line comment or single-line comment begins with the sequence // and ends at the end of the line.

You can place an end-of-line comment at the end of any line. Programmers use single-line comments for brief, line-by-line descriptions.

Everything you type after the // is ignored by the compiler. For example:

total = total * discountPercent; // calculate the discounted total

You can also place end-of-line comments on separate lines, like this:

// calculate the discounted total 

total = total * discountPercent;

You can place end-of-line comments in the middle of statements that span two or more lines. For example:

total = (total * discountPercent) // apply the discount first 
       + salesTax;                // then add the sales tax

Traditional comments

A traditional comment or multiline comment begins with the sequence /* , ends with the sequence */, and can span multiple lines. Anything between these two comment symbols is ignored by the compiler. As a general rule, programmers use multiline comments for longer remarks.

Here’s an example:

/*
This is a simple Java program.
Call this file "Example.java".
*/

You can even insert a comment between other Java programming elements, like this:

x = (y + /* a comment */ 10) / z;

One common use for traditional comments is to add a block of comment lines at the beginning of a class to indicate information about the class – such as what the class does, who wrote it, and so on.

That type of comment, however, is usually better coded as a JavaDoc comment.

multiline comment can’t be nested.

The following code won’t compile, for example:

/* 
    int x, y, z; 

    x = 10; 

    y = 15; 

    z = (y + /* a comment */ 25) / (x+y); 

*/

Here, we tried to comment out a range of lines with traditional comment.

Unfortunately, the */ sequence before 25 is interpreted as the end of the traditional comment that begins in the first line, so when the compiler encounters the */ sequence in line 6, it generates an error message.

Documentation comment

As mentioned, there are three types of comments defined by Java. You have already
seen two: single-line and multiline. The third type is called a documentation comment.
This type of comment is used to produce an HTML file that documents your program.
The documentation comment begins with a /** and ends with a */.

That’s it!
You have successfully completed the post. Do Share : )

Peace Out!

Also Read – Java Identifiers


Check Out Deals on -> Amazon , Flipkart , Myntra , Adidas , Apple TV , Boat , Canva , Beardo , Coursera , Cleartrip , Fiverr , MamaEarth , Swiggy


Leave a Reply

Your email address will not be published.