Learning Java
Custom Search

Previous Next

Comments

Comments are used to document the code. Commented lines are not executed. They are only there to be read by human eyes. There are 3 ways of entering comments.

The first way is by preceding a comment line or portion of a line with //

	// This section of code will calculate the 
	// net tax due after all adjustments and
	// expenses

	int i = 5;  // initialize the i value  

The second way is by surrounding the comment with /* ... */.

	/* This section of code will calculate the 
	   net tax due after all adjustments and
	   expenses */ 

	int i = 5;  /* initialize the i value  */ 

The third way is by surrounding the comment with /** ... */. This type of comment is used by programs such as javadoc provided in the SDK package with java and javac.  javadoc can be used to provide HTML documentation for others to use.

	/** This section of code will calculate the 
	    net tax due after all adjustments and
	    expenses */ 

	int i = 5;  /** initialize the i value  */ 

You can see the large quantity of documentation that will be created on the following simple code by running javadoc on it. If you have the commands java and javac, you should also have javadoc. You probably want to execute it in a separate directory since it creates quite a few files.

/** This little program is practically useless.
It is here only to show how javadoc reads these
comments and creates an HTML document.  */

public class ShowJavadoc {

    public static void main(String args[]) {

/** initialize and define my integers i and k. */

	int i = 3;
	int k;

        k = 100 + i;  /** showing addition */

        k = 100 * i;  /** showing multipication */

        k = 100 / i;  /** showing division */
    }
}

Previous Next

Steps In Learning

Contact us

Copyright © 2008      N. Nelson      All Rights Reserved