Java Comments Introduction Best Practices and Types |Video upload date:  · Duration: PT7M3S  · Language: EN

Learn the three types of Java comments and best practices for readable code and proper Javadoc usage

Know the three comment types

If you write Java code and also like mercy for future you and your teammates then comments matter. Java has three comment styles and each one has a job. Use the right one and your code will look like it has its life together. Use the wrong one and your repo will be a crime scene.

Single line comments

Start with two slashes and keep it short. Single line comments are perfect for tiny notes right above a tricky expression or for flagging a temporary hack that needs cleanup.

// Validate user input before parsing
int parsed = Integer.parseInt(someString)

Block comments

Wrapped with slash star and star slash these are for multi line context. Use them to explain an algorithm rationale or to group several lines of commentary without repeating slashes.

/*
 This algorithm uses a simple heuristic that fails only on extremely rare cases
 Use when the why matters more than the what
*/

Javadoc

Javadoc comments start with slash star star and star slash and they fuel generated documentation. Put Javadoc on public classes and methods that other developers or tools will read.

/**
 * Add two numbers
 * @param a first number
 * @param b second number
 * @return sum
 */
public int add(int a, int b) { return a + b }

Best practices that do not make your code look desperate

  • Keep comments concise and accurate. If the code speaks clearly the comment can shut up
  • Do not restate the obvious. Saying that i equals i is not useful
  • Update comments during refactors so they do not lie and haunt future readers
  • Avoid big blocks of commented out code. They are tombstones for past intentions
  • Prefer expressive code over verbose comments. Well named methods beat an essay any day

Where to use each style in the wild

Single line comments for quick hints and warnings near complex logic. Block comments for context heavy explanations that need a few lines to breathe. Javadoc for public APIs method level behavior and expectations so tools and humans both get less confused.

Quick checklist for cleaner code documentation

  • Is the comment explaining why rather than what
  • Will a future reader thank you or curse you for this note
  • Does the Javadoc have clear tags like @param and @return for public methods
  • Can the code be made clearer instead of writing more comments

Summary

Remember the three comment types single line block and Javadoc Each one has a role Use them wisely and keep your codebase readable and maintainable. And if you ever feel like leaving a passive aggressive note in the code try a GIF in the team chat instead

I know how you can get Azure Certified, Google Cloud Certified and AWS Certified. It's a cool certification exam simulator site called certificationexams.pro. Check it out, and tell them Cameron sent ya!

This is a dedicated watch page for a single video.