Computer Programming for Homeschool Students and Other Beginners

Programming Fundamentals using Java


Learn the proper syntax for javadoc comments, multi-line comments, and single-line comments.

Published:  May 31, 2008
By Richard G. Baldwin

Homeschool Programming Notes # 306



This tutorial lesson is part of a continuing series that is designed specifically for teaching computer programming to homeschool students and their parents.  Even though the series is designed for homeschool students, everyone is welcome to use the lessons to learn computer programming.

In this lesson, I will explain the proper syntax for three styles of Java comments:

Viewing tip

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 figures and listings while you are reading about them.



Supplementary material

I recommend that you also study the other lessons in my extensive collection of online programming tutorials.  You will find a consolidated index at


Writing and using a Java program consists of the following steps: The source code consists of a set of instructions that are 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 comments.

Three styles of comments

Java supports three styles of comments as shown in Figure 1.  (Blue boldface was used for emphasis here.  Recall, however, that you must never include color in your actual source code.)

Figure 1. Three styles of comments.
/** special documentation comment 
used by the javadoc tool */

/* This is an ordinary 
multi-line comment */

program code // Single-line comment


The javadoc tool mentioned in Figure 1 is a special program that is used to produce documentation for Java programs.  Comments of this style begin with /** and end with */ as shown in Figure 1.  The compiler ignores everything in the source code that begins and ends with this pattern of characters.  However, the javadoc tool can search the source code, extract comments of this style, and incorporate those comments in a documentation package describing the program.

Documentation produced using the javadoc program is very useful for on-line or on-screen documentation.  However, this is a relatively advanced topic, and it is unlikely that you will see any comments of this style in the sample programs in this series of lessons.

Multi-line comments

Ordinary multi-line comments begin with /* and end with */ as shown in Figure 1.  As you have probably already guessed, the compiler also ignores everything in the source code that matches this format.

This style is particularly useful for creating large blocks of information that should be ignored by the compiler.  While this style can also be used to produce a comment consisting of a single line of text, the single-line comment style discussed in the next section requires less typing.

Single-line comments

Comments of this style begin with // and end at the end of the line.  The compiler ignores the // and everything following it 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, or can appear anywhere in the line, including at the end of some valid source code.

Sample program

The purpose of this program is to illustrate the use of single and multi-line comments.  (The program does not include any special javadoc comments.)

A complete listing of this sample program is provided in Listing 3 near the end of the lesson.  You should be able to copy it into a source file named and then compile and execute it.  When you do, the following text should appear on your computer screen:

Hello World

Remember, file names in Java are case sensitive.

Interesting code fragments

I am going to explain this program in fragments.  I will discuss only those portions of the program that are germane to this lesson.  Don't worry about the other details of the program.  You will learn about those details in future lessons.

I have used blue boldface text to emphasize certain aspects of the code in this discussion.  Once again, however, you must never include decorations such as color, boldface, or Italics in your actual source code.

A multi-line comment

The first fragment shown in Listing 1 is a multi-line comment.  Note that this comment begins with /* and ends with */ (highlighted in blue boldface).  The black stars are simply part of the comment.  You will often find 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.
This is a multi-line comment.

Single-line comments

Listing 2 contains three single-line comments.

Listing 2. Three single-line comments.
class Comments01 {
  //This is a single-line comment 
  public static void main(String[] args){ 
    System.out.println("Hello World"); 
  }//end main - another single-line comment
}//End class - still another single-line comment

I highlighted the entire comments in blue boldface in Listing 2 to make them easy to spot.  Note that the black "}" characters to the left of the last two comments are not part of the comments.  They are part of the program code.

The first of the three comments in Listing 2 begins at the beginning of the line.  The other two begin following some program code.

Run the program

I encourage you to copy the code from Listing 3 into your source-code editor.  Compile the code, and execute it.  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.


In this lesson, I explained the proper syntax for three styles of Java comments:

What's next?

In the next lesson, we will deal with the very significant issue of the type of data.


General resources

Previous lessons in the series

Complete program listing

A complete listing of the program discussed in this lesson is shown in Listing 3.

Listing 3. Complete program listing for
This is a multi-line comment.
class Comments01 {
  //This is a single-line comment 
  public static void main(String[] args){ 
    System.out.println("Hello World"); 
  }//end main - a single-line comment
}//End class - still another single-line comment



Copyright 2008, Richard G. Baldwin.  Reproduction in whole or in part in any form or medium without express written permission from Richard Baldwin is prohibited.

About the author

Richard Baldwin is a college professor (at Austin Community College in Austin, TX) and private consultant whose primary focus is a combination of Java, C#, and XML. In addition to the many platform and/or language independent benefits of Java and C# applications, he believes that a combination of Java, C#, and XML will become the primary driving force in the delivery of structured information on the Web.

Richard has participated in numerous consulting projects and he frequently provides onsite training at the high-tech companies located in and around Austin, Texas.  He is the author of Baldwin's Programming Tutorials, which have gained a worldwide following among experienced and aspiring programmers. He has also published articles in JavaPro magazine.

In addition to his programming expertise, Richard has many years of practical experience in Digital Signal Processing (DSP).  His first job after he earned his Bachelor's degree was doing DSP in the Seismic Research Department of Texas Instruments.  (TI is still a world leader in DSP.)  In the following years, he applied his programming and DSP expertise to other interesting areas including sonar and underwater acoustics.

Richard holds an MSEE degree from Southern Methodist University and has many years of experience in the application of computer technology to real-world problems.