To comprehend any programming language, there are
several kind of comments which are used. These comments are advantageous in the
sense that they make the programmer feel convenient to grasp the logic of the
program. Although these comments are ignored by the Java compiler, they are
included in the program for the convenience of the user to understand it.
To provide the additional information about the code, use
comments. These comments give the overview of the code in the form of the
information which is not available in the code itself.
There are three types of comments used in Java. These are:
1. // text
To add a comment to the program, we can use two slashes characters i.e. //. The line starting from slashes to the end is considered as a comment. We can write only a single line comment use these slashes. For instance
// This comment extends to the end of the line.
// This type of comment is called a "slash-slash" comment
2. /* text */
To add a comment of more than one line, we can precede our comment using /*. The precise way to use this is to start with delimiter /* and end with delimiter */. Everything in between these two delimiters is discarded by the Java compiler. For instance
/* This comment, a "slash-star" comment, includes multiple lines.
* It begins with the slash-star sequence (with no space between
* the '/' and '*' characters) and extends to the star-slash sequence.
Slash-star comments may also be placed between any Java tokens:
int i = /* maximum integer */ Integer.MAX_VALUE;
3. /** documentation */
This is a special type of comment that indicates documentation comment. This type of comment is readable to both, computer and human. To start the comment, use /** instead of /* and end with */. This type of comment is a documentation which is interpreted as an official document on how the class and its public method work. For instance
* These are used to extract documentation from the Java source.
Comments in String Literals
Comments occurring in string literals are not parsed as comments. Like,
String text = "/* This is not a comment */";
Unicode Characters in Comments
Remember that Java still interprets Unicode sequences within comments. For example -:
/* This is a comment. \u002a\u002f
String statement = "This is a comment.";
and is lexically equivalent to
/* This is a comment. */
String statement = "/* This is a comment. */";
(The '*' character is Unicode 002A and the '/' character is Unicode 002F).
This also applies to newline characters in slash-slash comments.
//This is a single line comment \u000a This is code
That is because the \u000a is the Unicode for a new line, here the compiler thinks that you have added a new line.
This may be useful when declaring more than one thing on a line and you still wish to use // type comments
int x = 0; //X is the value of the carr \u000a int y=0; //Y is the intrest
Rs. 20,000 US$ 300
Today: Rs. 10,000 US$150
Course Duration: 30 hrs
Posted on: April 17, 2011 If you enjoyed this post then why not add us on Google+? Add us to your Circles