Codectionary / Developer documentation / Java

Comments & Javadoc

Java supports single-line comments with //, multi-line comments with /* */, and a special documentation format called Javadoc, written as /** */. Javadoc comments support tags like @param, @return, and @throws, and can be processed by the javadoc tool to automatically generate HTML API documentation - the same format used for Java's own official standard library docs.

Syntax

// single-line\n/* multi-line */\n/** Javadoc */

Examples

Single-Line and Multi-Line Comments

The two basic comment styles for explaining code.

// Calculate the area of a rectangle
int width = 5;
int height = 3;

/*
 * This is a multi-line comment.
 * It can span several lines and is often
 * used for longer explanations.
 */
int area = width * height;
System.out.println(area);

Javadoc Comments

Documenting a method's purpose, parameters, and return value in a format tools can process.

public class MathUtils {
    /**
     * Calculates the average of two numbers.
     *
     * @param a the first number
     * @param b the second number
     * @return the average of a and b
     */
    public static double average(double a, double b) {
        return (a + b) / 2;
    }
}

Commenting Out Code

Temporarily disabling a line while debugging.

int total = 0;
for (int i = 1; i <= 10; i++) {
    total += i;
    // System.out.println("Running total: " + total);  // disabled for now
}
System.out.println(total);

Best practices

  • Write Javadoc comments for every public class and method so IDEs can show helpful tooltips and documentation tools can generate API docs
  • Explain *why* code does something in comments, not *what* it does - the code itself should already communicate the "what"
  • Keep comments up to date - an outdated comment that contradicts the code is more harmful than no comment at all
  • Use @param, @return, and @throws tags consistently in Javadoc so generated documentation is complete and useful

At a glance

Purpose
General-purpose application development
File extension
.java
Runs in
Java Virtual Machine
Usually used with
JDK and Java libraries

Specifications & further reading

Related Java documentation

Variables & Primitive Data Types
Java is a statically-typed language, meaning every variable must be declared with an explicit type before use, and that type cannot change afterward. Java has eight primitive types - byte, short, int, long, float, double, char, and boolean - which store simple values directly rather than references, making them fast and memory-efficient. Anything beyond primitives (String, arrays, custom classes) is a reference type instead.
System.out.println / print / printf
System.out is Java's standard output stream, and it exposes three main methods for writing text to the console. println() prints its argument followed by a newline, print() prints without adding a newline, and printf() gives C-style formatted output using format specifiers like %d, %s, and %.2f for precise control over how values are displayed.
Arithmetic & Assignment Operators
Java provides the standard arithmetic operators for numeric computation: +, -, *, / for division, and % for the remainder (modulus). It also has compound assignment operators (+=, -=, etc.) that combine an operation with assignment, along with increment (++) and decrement (--) operators in both prefix and postfix forms, which subtly differ in when the value is updated relative to being used.
Comparison & Logical Operators
Comparison operators (==, !=, <, >, <=, >=) compare two values and evaluate to a boolean. Logical operators (&&, ||, !) combine or invert boolean expressions, with && and || short-circuiting - meaning the second operand is skipped entirely if the first already determines the result. Importantly, == compares object references for non-primitive types like String, so .equals() should be used to compare their actual content.