Comments in Java
Learn how to write clean, readable, and professional Java code using single-line, multi-line, and documentation (Javadoc) comments.
Single-line Comments
Single-line comments start with `//` and are best for short explanations, reminders, or clarifying what a specific line does.
1. Basic Single-line Comment
Everything after `//` on that line is ignored by the Java compiler.
2. Inline Comment (End of Line)
Inline comments are placed at the end of a line to explain that line quickly.
3. Debug Notes / TODOs
Common practice: use keywords TODO, FIXME, NOTE so they can be searched easily in IntelliJ / VS Code.
Multi-line Comments
Multi-line comments start with `/*` and end with `*/`. They are useful when you want to explain a block of logic or temporarily disable a block of code.
1. Basic Multi-line Comment
Multi-line comments can span across many lines and are great for explaining bigger blocks.
2. Temporarily Disable a Code Block
Useful during debugging when you want to stop some code from running without deleting it.
3. Warning / Big Notes
Many teams use multi-line comments for warnings or important notes at the top of a file.
Documentation Comments (Javadoc)
Documentation comments use `/** ... */` (notice the double asterisk). This style is called Javadoc and is the standard in Java projects. It helps IDEs (like IntelliJ IDEA) show method docs on hover and lets you generate HTML API documentation with the `javadoc` tool.
1. Documenting a Method
Use `@param` to describe parameters and `@return` to describe the return value.
2. Documenting a Class
Place the Javadoc comment directly above the class declaration to describe its purpose.
3. Useful Javadoc Tags
These tags make your codebase self-documented and easier to maintain.