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.
//)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
/* ... */)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;
}
/** ... */)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;
}
}
| Javadoc Tag | Applies To | Purpose & Description |
|---|---|---|
@param <name> | Methods, Constructors | Describes the role, expected data, and constraints of an argument. |
@return | Methods (non-void) | Explains what the method yields upon successful completion. |
@throws / @exception | Methods, Constructors | Documents checked or unchecked exceptions thrown under error conditions. |
@see | Classes, Methods | Provides a cross-reference link to another related class or method. |
@author | Classes, Interfaces | Specifies author name. |
@version | Classes, Interfaces | Specifies software version or release tag. |
@deprecated | Any element | Warns that the API is obsolete and indicates replacement methods. |
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.
Now let us examine methods, parameters, return types, and method overloading. Proceed to: Java Methods & Method Overloading →
Accelerate your Java mastery by studying these tightly connected core tutorials:
General language formatting.
Documenting parameters and returns.
Running the javadoc utility.
Cheat sheet of keywords.