Inside Collection (Course): Object-Oriented Programming (OOP) with Java
Summary: This module explains Java comments.
This module is part of a sub-collection of modules designed to help you learn to program computers.
It explains Java comments.
In addition to an Internet connection and a browser, you will need the following tools (as a minimum) to work through the exercises in these modules:
The minimum prerequisites for understanding the material in these modules include:
I recommend that you open another copy of this document in a separate browser window and use the following links to easily find and view the images and listings while you are reading about them.
Producing and using a Java program consists of the following steps:
The source code consists of a set of instructions that will later be presented to a special program called a compiler for the purpose of producing a program that can be executed. In other words, when you write the source code, you are writing instructions that the compiler will use to produce the executable program.
Some things should be ignored
Sometimes, when you are writing source code, you would like to include information that may be useful to you, but should be ignored by the compiler. Information of that sort is called a comment .
Three styles of comments
Java supports the three styles of comments shown in Image 1 .
| Image 1: Three styles of comments. | |
|---|---|
|
The javadoc tool
The javadoc tool mentioned in Image 1 is a special program that is used to produce documentation for Java program. Comments of this style begin with /** and end with */ as shown in Image 1 .
The compiler ignores everything in the source code that begins and ends with this pattern of characters. Documentation produced using the javadoc program is very useful for on-line or on-screen documentation.
Multi-line comments
Multi-line comments begin with /* and end with */ as shown in Image 1 . As you have probably already guessed, the compiler also ignores everything in the source code that matches this format. (A javadoc comment is simply a multi-line comment insofar as the compiler knows. Only the special program named javadoc.exe cares about javadoc comments.)
The multi-line comment style is particularly useful for creating large blocks of information that should be ignored by the compiler. This style can be used to produce a comment consisting of a single line of text as well. However, the single-line comment style discussed in the next section requires less typing.
Single-line comments
Single-line comments begin with // and end at the end of the line. The compiler ignores the // and everything following the slash characters to the end of the line.
This style is particularly useful for inserting short comments throughout the source code. In this case, the // can appear at the beginning of the line as shown in Image 1 , or can appear anywhere in the line, including at the end of some valid source code (also shown in Image 1 ) .
The purpose of the program named Comments01 , which is shown in Listing 3 near the end of the module, is to illustrate the use of single and multi-line comments. The program does not contain any javadoc comments.
The commands for a batch file that you can use to compile and run this program are provided in Listing 4 .
When you compile and run the program, the following text should appear on your command-line screen:
Hello World
I will explain this program in fragments, and will explain only those portions of the program that are germane to this module. Don't worry about the other details of the program at this time. You will learn about those details in future modules.
A multi-line comment
Listing 1 , shows a multi-line comment, which consists of three lines of text.
As required, this multi-line comment begins with /* and ends with */. The extra stars on the third line are simply part of the comment.
You will often see formats similar to this being used to provide a visual separation between multi-line comments and the other parts of a program.
| Listing 1: A multi-line comment. |
|---|
|
Single-line comments
Listing 2 shows three single-line comments. Can you spot them? Remember, single-line comments begin with //.
| Listing 2: Three single-line comments. |
|---|
|
One of the comments in Listing 2 starts at the beginning of the line. The other two comments follow some program code.
I encourage you to run the program that I presented in this lesson to confirm that you get the same results. Experiment with the code, making changes, and observing the results of your changes. Make certain that you can explain why your changes behave as they do.
Listing 3 contains a complete listing of the program named Comments01 .
| Listing 3: The program named Comments01. |
|---|
|
Listing 4 contains the commands for a batch file that can be used to compile and run the program named Comments01 .
| Listing 4: Batch file to compile and run the program named Comments01. |
|---|
|
This section contains a variety of miscellaneous information.
Financial : Although the Connexions site makes it possible for you to download a PDF file for this module at no charge, and also makes it possible for you to purchase a pre-printed version of the PDF file, you should be aware that some of the HTML elements in this module may not translate well into PDF.
I also want you to know that, I receive no financial compensation from the Connexions website even if you purchase the PDF version of the module.
In the past, unknown individuals have copied my modules from cnx.org, converted them to Kindle books, and placed them for sale on Amazon.com showing me as the author. I neither receive compensation for those sales nor do I know who does receive compensation. If you purchase such a book, please be aware that it is a copy of a module that is freely available on cnx.org and that it was made and published without my prior knowledge.
Affiliation : I am a professor of Computer Information Technology at Austin Community College in Austin, TX.
-end-