next up previous contents
Next: Variables declaring Up: 1 Programming Basics Previous: Running a program on   Contents

Comments

Comments in a computer program are notes you make to yourself. They will be ignored by the compiler and do not affect the way the program runs. For this reason they must be marked. There are three ways to do this depending on the circumstances. The first looks like this:

/**
 *   Purpose: describe what the program does,
 *   add any extra lines needed using an asterisk
 */
This form should be used at the beginning of all your files and give any necessary information about your code, including its purpose, your name, the date and the exercise number where applicable. Especially with longer files you may find it helpful to include a modification history here as well, stating what changes were made and when.

The other two types of comment are:

// comments go here, rest of line is ignored

/* comments here may use
   more than one line
   and finish with */

When // appears on a line, everything to the right up to the end of the line is ignored by the compiler. Java code can appear to the left of //. Longer comments may take the second form shown. Anything between /*...*/ is considered to be a comment by the compiler and ignored. Unlike //, the comment can span several lines.

As // comments may be placed within /*...*/, you may try putting the latter around a block of code in your program that you think is causing an error or exception, as long as the block of code only includes // comments3. Two sets of /*...*/ comments can not be nested within each other.

It is important that you add sufficient comments to your programs so that it is clear what they do and how they work. When someone else reads your program they cannot read your mind, so they will need more help to understand it than you do. You will see more clearly how comments are put to use as you progress through the course.


next up previous contents
Next: Variables declaring Up: 1 Programming Basics Previous: Running a program on   Contents
RHUL Dept. of Physics