Java: Comments
End of line comments:
// comment
Convenient but if you write code after the //, it all gets grayed out.
Block comments:
/* comment */
Advantage is that they can appear anywhere, span multiple lines, etc.
And here’s a line of code with a comment in the middle of it:
int counter /* comment */ = 0; Please don’t do that. It’s legal, but it’s poor coding practice, because it makes the code harder to read. – Java 7 for Absolute Beginners
Note that Javadoc comments are similar to block comments but with an extra asterisk at the beginning:
/** * This method makes a Cat object chase the given Mouse object. * @param mouse an instance of the Mouse class for our cat to chase */’ – Java 7 for Absolute Beginners
Really important tip: Don’t worry about line breaks when you’re writing comments. Press Command + Shift + F and it formats everything. You want to keep comments to about 80px. Get into the issue of line breaks with code. Sometimes line breaks with commands can be problematic – such as wrapping comment code.
About Tom Johnson
I'm an API technical writer based in the Seattle area. On this blog, I write about topics related to technical writing and communication — such as software documentation, API documentation, AI, information architecture, content strategy, writing processes, plain language, tech comm careers, and more. Check out my API documentation course if you're looking for more info about documenting APIs. Or see my posts on AI and AI course section for more on the latest in AI and tech comm.
If you're a technical writer and want to keep on top of the latest trends in the tech comm, be sure to subscribe to email updates below. You can also learn more about me or contact me. Finally, note that the opinions I express on my blog are my own points of view, not that of my employer.