☕ Java
Java Comments
Comments are the parts of your code the compiler ignores — but they're often the parts human readers need most. Java has three types of comments, each with a specific purpose. Here's when and how to use each one, and the difference between comments that help and comments that just add noise.
Three Types of Comments in Java
Java has exactly three comment styles:
- Single-line comments — // — for brief inline notes
- Multi-line comments — /* */ — for longer explanations spanning multiple lines
- Javadoc comments — /** */ — for generating official API documentation
All three are completely ignored by the Java compiler — they produce no bytecode and have zero effect on runtime behavior. Their only audience is humans — the developers reading your code now and in the future.
Single-Line Comments
The double slash (//) starts a comment that runs to the end of the line. Everything after // on that line is ignored by the compiler.
Java
// This is a standalone single-line comment
int age = 25; // this is an inline comment — appears after code on the same line
// Multi-word explanation on one line:
// Calculate compound interest using principal * (1 + rate)^years
// Temporarily disable code during debugging:
// System.out.println("debug: value = " + value);
// DON'T — redundant comments that just restate the code:
int count = 0; // set count to zero ← states the obvious
i++; // increment i ← states the obvious
// DO — comments that explain WHY, not WHAT:
int retryCount = 3; // max retries before circuit breaker opens
i += 2; // skip even indices — only processing odd positionsMulti-Line Comments
Multi-line comments start with /* and end with */. Everything between those delimiters is ignored, regardless of how many lines it spans. They're used for longer explanations that would be awkward squeezed onto one line.
Java
/* This is a multi-line comment.
It can span as many lines as needed.
The compiler ignores everything between the delimiters. */
/*
* Common style: asterisks on each line for alignment.
* Not required, just a widely followed convention.
* Makes multi-line blocks visually clear.
*/
/* Explaining a non-obvious algorithm:
*
* This uses Floyd's cycle detection algorithm (tortoise and hare).
* Two pointers move at different speeds — if there's a cycle,
* the fast pointer will eventually lap the slow one.
* Time: O(n), Space: O(1)
*/
ListNode slow = head;
ListNode fast = head;
/* Temporarily comment out a block during debugging: */
/*
processPayment(order);
sendConfirmationEmail(order);
updateInventory(order);
*/Javadoc Comments
Javadoc comments start with /** (two asterisks) and end with */. They're placed directly before class, method, or field declarations, and the javadoc tool processes them into HTML API documentation — the same format as the official Java API docs at docs.oracle.com.
Every public class and public method in a library or API should have a Javadoc comment.
Java
/**
* Represents a bank account with basic deposit and withdrawal operations.
*
* <p>Accounts are identified by a unique account number assigned at creation.
* All monetary values are in the account's declared currency.</p>
*
* @author Alice Johnson
* @version 2.1
* @since 1.0
*/
public class BankAccount {
/**
* The current balance of this account.
* Always non-negative; withdrawals that would make it negative are rejected.
*/
private double balance;
/**
* Deposits the specified amount into this account.
*
* @param amount the amount to deposit; must be greater than zero
* @throws IllegalArgumentException if {@code amount} is zero or negative
*/
public void deposit(double amount) {
if (amount <= 0) throw new IllegalArgumentException("Amount must be positive");
this.balance += amount;
}
/**
* Returns the current balance of this account.
*
* @return the current balance, always >= 0
*/
public double getBalance() {
return balance;
}
}Javadoc Tags — The Full Reference
Javadoc tags are processed by the javadoc tool and appear as structured sections in the generated HTML documentation:
Java
/**
* @param name description of a method parameter
* @return description of the return value
* @throws ExceptionType conditions under which this exception is thrown
* @exception ExceptionType older synonym for @throws
*
* @author name of the author
* @version current version string
* @since version when this was introduced
* @deprecated explain why deprecated and what to use instead
*
* @see ClassName link to another class
* @see ClassName#method link to a specific method
* @see <a href="url">text</a> external link
*
* @serial describes serializable fields
* @serialData describes serialization data
*
* Inline tags (used inside descriptions):
* {@code value} renders as monospace code — won't interpret HTML
* {@link ClassName#method} hyperlink to another element
* {@linkplain ClassName text} hyperlink with custom display text
* {@inheritDoc} copies the Javadoc from the overridden method
* {@literal <text>} renders angle brackets literally (for generics in prose)
*/Generating Javadoc Documentation
Run the javadoc tool from the command line to generate HTML documentation from your Javadoc comments:
Shell
# Generate docs for a single file:
javadoc BankAccount.java
# Generate docs for all files in a package:
javadoc com.example.banking
# Generate with output directory and metadata:
javadoc -d docs/ -author -version -windowtitle "Banking API" src/**/*.java
# Maven generates Javadoc as part of the build:
mvn javadoc:javadoc
# Output appears in: target/site/apidocs/index.html
# Gradle:
./gradlew javadoc
# Output appears in: build/docs/javadoc/index.htmlWhat to Comment and What Not To
The most important commenting skill is knowing what NOT to comment. Useless comments are worse than no comments — they add noise and go stale when code changes.
Java
// ── GOOD COMMENTS — explain the non-obvious ─────────────────────
// Using a LinkedList here instead of ArrayList because this queue
// has frequent insertions at the front — O(1) vs O(n)
LinkedList<Task> taskQueue = new LinkedList<>();
// Magic number with explanation:
Thread.sleep(429); // 429 = HTTP Too Many Requests — wait before retry
// Business rule that isn't obvious from code:
if (user.getAge() >= 18 && user.getCountry().equals("IN")) {
// KYC required for Indian users 18+ per RBI regulation 2021
triggerKYCVerification(user);
}
// ── BAD COMMENTS — restate what code already says ────────────────
// Get the user's name
String name = user.getName(); // ← obvious from code
// Loop through the list
for (String item : list) { } // ← obvious from code
// Add a to b and store in c
int c = a + b; // ← obvious from code
// Increment counter
counter++; // ← obvious from code
// ── OUTDATED COMMENTS — worse than no comments ────────────────────
// Returns the user's email ← comment says one thing
public String getUsername() { } // code does another
// Fix: delete the comment or update it — never leave contradictionsRelated Topics in Introduction
What is Java?
Java is a high-level, object-oriented programming language built on one killer idea: write your code once, and run it on any device — Windows, Mac, Linux, phone, smartwatch, you name it. No rewrites needed.
Features of Java
Java didn't become one of the world's most used languages by accident. From running on any device to handling millions of users simultaneously, here's what makes Java genuinely powerful — and why companies keep betting on it.
Uses of Java
Java powers everything from Android apps to banking systems, from Netflix's backend to NASA's mission control. Here's where Java is actually used in the real world — and why it keeps showing up in the most critical systems on the planet.
Java Editions (Java SE, EE, ME)
Java isn't one-size-fits-all. It comes in three distinct editions — each built for a different environment. Whether you're building a desktop app, a banking backend, or firmware for a SIM card, there's a Java edition designed exactly for that job.