Comments
A comment is a sequence of characters
ignored by the Java compiler and interpreter. In Java, comments have
one of these three forms:
//
The rest of this line is a comment
/* Everything
in this
section is
a comment */
/** Everything
in this section
is a documentation
comment */
The computer never gets to process anything
marked as a comment, so comments can say anything you like. Different authorities
have different ideas about the amount of comments to use, but you should
use some ?rather than none. Comments are what will make it possible
for you to understand your program if you look at it again after you thought
you had finished it. They will also make it easier for another person (e.g.,
one of your co-workers) to understand your program.
The use of extensive comments
in computer programs is becoming less common in modern practice, although
some authorities still recommend it. The number of comments in Hello1.java
is excessive, and one would not normally comment a program to this extent
except for educational purposes. The reason for this is that while comments
can give human readers a good idea of what different parts of a program
will do when executed, what is hard to understand is the overall structure
of the program. This is where techniques like object-oriented design can
help, as they document the large-scale structure of a program.
The difference between a standard comment
(//, /*) and a `documentation comment' (/**) is that the latter is considered
to be part of the overall explanation of how a program works. Software
exists that will extract the documentation comments and make them into
a report. The distinction is not that important in the context of this
course, but anyone who is interested in programming professionally should
consider finding out about the standard documentation generation tooljavadoc
(which is part of the JDK package).
Consider the
following program:
import
java.io.*;
class commentme
{
public
static void main( String args[] )
{
// display some text on screen
System.out.println("line 1");
System.out.println("line 2");
System.out.println("line 3");
System.out.println("line 4");
}
}
At present there is a single comment in
this class:
// display
some text on screen
the 4 lines following this comment
will display 4 lines of text.
line 1
line 2
line 3
line 4
Press any key to continue ...
One common technique when debugging
a program is to comment out lines, to reduce the number of statements
that are compiled and executed. Thus one can isolate parts of a program
to help locate errors. By the phrase 'comment out' it is meant that program
statements are edited with comments so that they are ignored by the compiler
and interpreter. For example, we could comment out the first output statement
as follows:
import
java.io.*;
class
commentme
{
public
static void main( String args[] )
{
// display some text on screen
// System.out.println("line 1");
System.out.println("line 2");
System.out.println("line 3");
System.out.println("line 4");
}
}
The output on screen is now:
line 2
line 3
line 4
Press any key to continue ...
since the first output line has been
ignored by the compiler (and so there is no compiled 'line 1' output statement
to be interpreted).
Likewise we can comment out larger
sections of code using the two-part `slash-asterisk' comment:
/* any characters
between the slash-asterisk and asterisk-slash are ignored, even on different
lines */
For example we can comment out the
3rd and 4th output lines of our program as follows:
class
commentme
{
public
static void main( String args[] )
{
// display some text on screen
System.out.println("line 1");
System.out.println("line 2");
/* System.out.println("line 3");
System.out.println("line 4");
*/
}
}
The output on screen is now:
line 1
line 2
Press any key to continue ...
since the third and fourth output lines
have been ignored by the compiler (and so there are no compiled 'line 3'
or 'line 4' output statements to be interpreted).
However, perhaps the most useful feature
of comments is that they facilitate the recording of ideas and design summaries
of those writing programs. Such comments are an important source of information
about a program, since they can be written and updated at the same time
the program is written (as opposed to attempting to keep written records
up to date with a changing program). See the listing for Hello1.java for
an example of a program with an awful lot of description comments in it.
Back
to top
.
RITSEC - Global Campus
Copyright © 1999 RITSEC- Middlesex
University. All rights reserved.
webmaster@globalcampus.com.eg
|