….
💬 Java Comments – Writing Clearer Code
In programming, writing code that works is great — but writing code that others (or your future self) can understand is even better. That’s where comments come in.
At GoNimbus, we encourage clean, readable, and well-documented code from the start. Let’s dive into how comments in Java help you document your logic, plan your program, and debug with ease.
🟩 What Are Comments?
Comments are ignored by the Java compiler. They don’t affect your program’s output — they exist purely to help humans understand the code.
They are useful for:
- 🧠 Explaining why a piece of code exists
- 🧪 Temporarily disabling code during testing
- 👥 Collaborating with other developers
- 📝 Keeping notes on TODOs or bugs
🧍 Single-Line Comments
Use //
to write a comment that ends at the end of the line.
✅ Example:
// This line prints a greeting
System.out.println("Hello from GoNimbus!");
You can also add it after a line of code:
System.out.println("Java is fun!"); // Output a message
📄 Multi-Line Comments (Comment Blocks)
When you need to write longer notes or comment out several lines, use:
/*
This is a multi-line comment.
Everything between these markers is ignored.
*/
✅ Example:
/* The following code prints three lines of text
to demonstrate multi-line commenting */
System.out.println("Line 1");
System.out.println("Line 2");
System.out.println("Line 3");
You can even comment out blocks of code while debugging:
/*
System.out.println("This will not run");
System.out.println("Still not running");
*/
🆚 Single-Line vs Multi-Line Comments
Type | Syntax | Use When… |
---|---|---|
Single-Line | // comment | You need a short note or comment beside a line |
Multi-Line | /* comment */ | You want to explain logic, disable multiple lines, or write longer descriptions |
💡 GoNimbus Best Practices
- ✅ Use comments to explain why, not what — the code already shows what it’s doing.
- 🛠️ Avoid over-commenting simple code like
int x = 5; // declare x as 5
— it’s redundant. - 📌 Keep comments up-to-date — outdated comments can confuse developers.
- 🧹 Clean up commented-out code before finalizing your project.
🔜 Up Next: Java Variables and Data Types
You’ve mastered printing and commenting — now it’s time to learn how to store data using variables and Java’s rich set of data types.