Java Tutorial

Operators

Flow Control

String

Number and Date

Built-in Classes

Array

Class and Object

Inheritance and Polymorphism

Exception Handling

Collections, Generics and Enumerations

Reflection

Input/Output Stream

Annotation

Javadoc (Documentation Comments)

Javadoc is a tool for generating API documentation in HTML format from Java source code. Javadoc comments are special comments that start with /** and end with */, and are used to document Java code.

Here is an example of a Javadoc comment in Java:

/**
 * This class represents a person with a name and an age.
 */
public class Person {
    /**
     * The name of the person.
     */
    private String name;

    /**
     * The age of the person.
     */
    private int age;

    /**
     * Creates a new person with the specified name and age.
     * @param name the name of the person
     * @param age the age of the person
     */
    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    /**
     * Returns the name of the person.
     * @return the name of the person
     */
    public String getName() {
        return name;
    }

    /**
     * Returns the age of the person.
     * @return the age of the person
     */
    public int getAge() {
        return age;
    }
}

In this example, we have used Javadoc comments to document the Person class and its methods. The Javadoc comments include a description of the class and its methods, as well as information about the parameters, return values, and exceptions that may be thrown.

Javadoc comments can include the following tags:

  • @param: Describes a parameter of a method or constructor.
  • @return: Describes the return value of a method.
  • @throws: Describes an exception that may be thrown by a method or constructor.
  • @deprecated: Indicates that a class, method, or field is deprecated and should not be used.
  • @see: Provides a link to another class, method, or field.

Javadoc comments can also include HTML tags for formatting text and creating links.

To generate API documentation using Javadoc, you can use the javadoc command-line tool, which is included with the Java Development Kit (JDK). The javadoc tool generates HTML documentation for all public classes and their public and protected members in the specified packages and source files. The generated documentation includes the Javadoc comments and their associated tags, as well as class and method signatures and inheritance diagrams.

Javadoc comments are an important part of writing clear, readable, and maintainable Java code. By documenting your code with Javadoc comments, you can help other developers understand how your code works and how to use it correctly.

  1. Javadoc tags and their meanings

    Javadoc tags provide additional information in comments that is used to generate documentation. Common tags include @param, @return, @throws, and @see.

    /**
     * Calculates the sum of two numbers.
     *
     * @param a The first number.
     * @param b The second number.
     * @return The sum of the two numbers.
     */
    public int add(int a, int b) {
        return a + b;
    }
    
  2. Including code examples in Javadoc comments

    /**
     * Example of using the add method:
     * <pre>{@code
     * int result = add(2, 3);
     * System.out.println(result); // Output: 5
     * }</pre>
     */
    public static void main(String[] args) {
        // Example usage
    }
    
  3. Javadoc tool command-line options and usage

    The javadoc tool is used to generate documentation. Example usage:

    javadoc -d docs -sourcepath src -subpackages com.example
    
  4. Customizing Javadoc output format in Java

    Customizing Javadoc output can be achieved using the -doclet option with a custom doclet.

    javadoc -doclet com.example.CustomDoclet -sourcepath src -subpackages com.example
    
  5. Linking to other classes and methods in Javadoc

    /**
     * Calculates the sum of two numbers.
     *
     * @param a The first number.
     * @param b The second number.
     * @return The sum of the two numbers.
     * @see Math#addExact(int, int)
     */
    public int add(int a, int b) {
        return Math.addExact(a, b);
    }
    
  6. Javadoc and code versioning in Java projects

    Including version information in Javadoc:

    /**
     * Calculates the sum of two numbers.
     *
     * @version 1.0
     * @param a The first number.
     * @param b The second number.
     * @return The sum of the two numbers.
     */
    public int add(int a, int b) {
        return a + b;
    }