Comments

There are three types of comments defined by Java.

  1. Single-line,
  2. Multiline and
  3. Documentation comment.

Single-line comment

Java single line comment starts from // and ends till the end of that line.


public class Main {
  // This is a single line comment.
  public static void main(String[] argv) {
  }

}

Multiline comment

Java multiline comment is between /* and */.


public class Main {
  /* This 
     is 
     a
     Multiline 
     comment.
  */
  public static void main(String[] argv) {
  }

}

Documentation comment.

Documentation comment is used to produce an HTML file that documents your program. A Javadoc comment occupies one or more lines of source code. The documentation comment begins with a /** and ends with a */. Everything from /** through */ is ignored by the compiler.

The following example demonstrates a Javadoc comment:


/** 
* Application entry point 
* 
* @param args array of command-line arguments passed to this method 
*/ 
public static void main(String[] args) 
{ 
// TODO code application logic here 
}

This example begins with a Javadoc comment that describes the main() method. /** and */ contains a description of the method, which could include HTML tags such as <p> and <code>/</code>, and the @param Javadoc tag (an @-prefixed instruction).

The following list identifies several commonly used tags:

  • @author identifies the source code's author.
  • @deprecated identifies a source code entity that should no longer be used.
  • @param identifies one of a method's parameters.
  • @see provides a see-also reference.
  • @since identifies the software release where the entity first originated.
  • @return identifies the kind of value that the method returns.

The following code has more documentation comments


/**
 * A simple class for introducing a Java application.
 * 
 * @author yourName
 */
public class HelloWorld {
  /**
   * Application entry point
   * 
   * @param args
   *          array of command-line arguments passed to this method
   */
  public static void main(String[] args) {
    System.out.println("Hello, world!");
  }
}

We can extract these documentation comments into a set of HTML files by using the JDK's javadoc tool, as follows:


javadoc HelloWorld.java

javadoc defaults to generating HTML-based documentation for public classes and public/protected members of these classes.

Home 
  Java Book 
    Language Basics  

Statement:
  1. Simplest if statement
  2. If else statement
  3. switch statement
  4. while loop
  5. do-while statement
  6. for Loop
  7. for each loop
  8. break to Exit a Loop
  9. continue
  10. return statement returns from a method.
  11. Comments