Last Updated: January 3, 2026
When you’re writing code, it’s easy to get wrapped up in the logic, the syntax, and the algorithms. But what about the context, the reasoning, and the purpose behind that code?
That’s where comments come into play.
Comments are like the breadcrumbs left behind for anyone (including your future self) trying to follow the trail of thought that led to the final product. They provide clarity, enhance readability, and offer insight into the developer's intent.
In Java, comments are pieces of text that are not executed by the Java Virtual Machine (JVM). They exist solely for the benefit of anyone reading the code. Comments can explain complex logic, clarify why certain decisions were made, and even remind future developers of the reasoning behind specific implementations.
There are three primary types of comments in Java:
Let’s break them down.
Single-line comments are created using two forward slashes (//). Everything following the slashes on that line is ignored by the compiler. This type of comment is perfect for short notes or explanations.
Here's a quick example to illustrate:
In the example above, the comments clarify what each line of code does. They’re short, to the point, and help anyone reading the code understand its purpose without diving into complex explanations.
Multi-line comments start with /* and end with */. These are beneficial for providing longer explanations or for commenting out sections of code during debugging.
Here’s how you can use multi-line comments:
In this example, the multi-line comment explains the overall purpose of the program. It can be particularly useful for providing context when working with larger chunks of code.
Javadoc comments are a specific type of multi-line comment that starts with /** and ends with */. They are used to document Java classes, methods, and fields for generating API documentation.
Let’s see how Javadoc comments are structured:
In this code, Javadoc comments describe the class, the main method, and the printNumber method. They provide essential information about what each component does, which is invaluable for anyone using or maintaining the code.
While comments are essential for maintaining clear and understandable code, it’s equally important to use them effectively. Here are some best practices to keep in mind:
Aim for clarity. A good comment should be understandable without needing to read the code it refers to. Avoid overly complex phrases or jargon.
Instead of describing what the code does, focus on explaining why it was written that way. This helps provide context that may not be immediately obvious from the code itself.
As your code evolves, so should your comments. Outdated or incorrect comments can mislead future developers and create confusion.
If you’ve made a particular decision that affects your code's logic, document it. This can save time for others trying to understand why a certain approach was taken.
Sometimes less is more. If the code is simple and self-explanatory, excessive comments can clutter the code and detract from readability.
Consider using documentation tools or linters that can help enforce comment standards within your team. This ensures consistency across your codebase.
While comments are helpful, there are some classic mistakes developers make that can lead to confusion or miscommunication.
It might be tempting to leave commented-out code in your files. However, this can lead to clutter. If you decide a piece of code is no longer needed, consider deleting it instead.
Don’t rely on comments to explain poorly written code. If a piece of code requires extensive commenting to be understood, it might be time to refactor it.
When reviewing code, pay attention to comments. They can provide context that’s essential for understanding the code’s intent and functionality.
In this chapter, we explored the different types of comments in Java: single-line, multi-line, and Javadoc comments. We discussed their purposes, best practices for effective commenting, and common pitfalls to avoid.
By adhering to these principles, you can significantly enhance the readability and maintainability of your code, making it easier for others (and yourself) to understand and collaborate on future projects.
In the next chapter, we will look at the importance of naming your variables and methods clearly, and how it complements the practices we’ve discussed here.