Translate

Sunday 10 June 2012

Types of Comments in Java

Consider the following sample program:

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

class Example
{
    //Your program begins with a call to main().
    public static void main(String args[])
   {
      System.out.println("This is a simple Java program.");
   }
}

The program begins with the following lines:
/*
This is a simple Java program.
Call this file "Example.java".
*/

This is a comment. The contents of the 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. In real applications, comments generally explain how some part of the program works or what a specific feature does.
Java supports 3 types of the comments. The one shown at the top of the program is called multiline comment. This type of comment must begin with /* and end with */. Anything between these two comment symbols is ignored by the compiler. As the name suggests, a multiple comment may be several lines long.

The second type of the comment supported by java is the single line comment, shown here:

    //Your program begins with a call to main().

A single line comment begins with a // and ends at the end of the line. As a general rule, programmers use multiline comments for longer remarks and single line comments for brief, line by line descriptions.

The third type of comment 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 */. Here is an example of a documentation comment for a class:

/**
* This class draws a bar chart.
* @author author_name
* @version 3.2
*/





No comments:

Post a Comment