Java Comments & Javadoc: Writing Professional Code Documentation

Comments are non-executable explanatory notes written inside source code to help humans understand the architectural rationale, algorithm constraints, and API contracts of the software.

The 3 Types of Comments in Java

1. Single-Line Comments (//)

Used for brief inline explanations or temporarily toggling a line of code during debugging. The compiler ignores everything from // to the end of that line:

// Define database connection timeout in milliseconds
int timeoutMs = 5000;
int maxRetries = 3; // Maximum reconnect attempts

2. Multi-Line Comments (/* ... */)

Used for longer narrative explanations spanning multiple lines:

/*
 * The following algorithm computes the greatest common divisor (GCD)
 * using Euclid's subtraction algorithm. It guarantees O(log(min(a,b)))
 * time complexity.
 */
public static int gcd(int a, int b) {
    while (b != 0) {
        int temp = b;
        b = a % b;
        a = temp;
    }
    return a;
}

3. Javadoc Comments (/** ... */)

Javadoc comments begin with /** and end with */. They are used immediately before class, interface, method, or constructor declarations to generate formal HTML API documentation:

/**
 * Represents a customer savings account with compound interest calculation.
 * 
 * @author Veewom Engineering Team
 * @version 2.1
 * @since Java 21
 */
public class SavingsAccount {
    private double balance;

    /**
     * Deposits funds into the savings account and updates current ledger.
     *
     * @param amount The monetary amount in USD to deposit (must be > 0).
     * @return The updated total account balance after deposit.
     * @throws IllegalArgumentException if the provided amount is negative or zero.
     */
    public double deposit(double amount) {
        if (amount <= 0) {
            throw new IllegalArgumentException("Deposit amount must be positive!");
        }
        this.balance += amount;
        return this.balance;
    }
}

Standard Javadoc Tags Reference

Javadoc Tag Applies To Purpose & Description
@param <name>Methods, ConstructorsDescribes the role, expected data, and constraints of an argument.
@returnMethods (non-void)Explains what the method yields upon successful completion.
@throws / @exceptionMethods, ConstructorsDocuments checked or unchecked exceptions thrown under error conditions.
@seeClasses, MethodsProvides a cross-reference link to another related class or method.
@authorClasses, InterfacesSpecifies author name.
@versionClasses, InterfacesSpecifies software version or release tag.
@deprecatedAny elementWarns that the API is obsolete and indicates replacement methods.

University Exam Corner (BCA / B.Sc CS)

Exam Question: "Can comments be nested in Java?"

Answer: No! Multi-line comments /* ... */ cannot be nested inside one another. When the compiler encounters the very first */ closing symbol, it terminates the comment immediately, causing any remaining code or trailing symbols to trigger compilation errors.

Next Step

Now let us examine methods, parameters, return types, and method overloading. Proceed to: Java Methods & Method Overloading →

Frequently Asked Questions (FAQ)

Q1. What is the difference between standard multi-line comments and Javadoc comments?
Standard multi-line comments (/* ... */) are purely for developer notes and are ignored by documentation tools. Javadoc comments (/** ... */) are structured comments containing special annotations (@param, @return) parsed by the 'javadoc' tool to generate formal HTML API documentation.
Q2. Are comments preserved in compiled .class bytecode files?
No! The javac compiler completely strips all single-line and multi-line comments during the lexical tokenization phase. Comments have zero impact on bytecode size, execution speed, or JVM memory.
Q3. What are the most essential Javadoc tags?
The most important tags are @param (documents method parameter purpose), @return (describes return value), @throws / @exception (documents possible exceptions), @author (class creator), and @since / @version (version release).

Explore Related Java Tutorials

Accelerate your Java mastery by studying these tightly connected core tutorials:

Java Syntax Rules →

General language formatting.

Java Methods Guide →

Documenting parameters and returns.

JDK Tooling Setup →

Running the javadoc utility.

Java Quick Reference →

Cheat sheet of keywords.

Share Share on Facebook Share on Twitter Share on LinkedIn Pin on Pinterest Share on Stumbleupon Share on Tumblr Share on Reddit Share on Diggit

You may also like this!