Skip to content
C

Comments

A comment is a line or block of text in your code that Java ignores when running the program. Comments are written purely for humans — to explain what the code does, so anyone reading it later understands the intention behind it.


1. What is a Comment?

A comment is a line or block of text in your code that Java ignores when running the program. Comments are written purely for humans — to explain what the code does, so anyone reading it later understands the intention behind it.

2. Why is it used?

Comments make code easier to understand, both for other developers and for your future self. When a program grows large, comments help explain tricky logic without needing to re-read every single line of code to figure out what's happening.

3. Real-Life Example

Think of sticky notes left on a recipe card, saying "reduce sugar if guests dislike sweet food." The recipe itself still works without the note, but the note helps whoever reads it later understand a special consideration behind a step.

4. Syntax

java
// This is a single-line comment /* This is a multi-line comment */ /** * This is a documentation comment (Javadoc) */

5. Example Program

java
public class CommentDemo { public static void main(String[] args) { // Printing a welcome message System.out.println("Comments make code clear!"); /* This line is just an example of a multi-line comment */ } }

Output:

Comments make code clear!

6. Key Points to Remember

  • Comments are ignored by the compiler and do not affect program execution.
  • Three types exist: single-line (//), multi-line (/* */), and documentation (/** */).
  • Good comments explain "why," not just "what," since the code itself already shows "what."

Mock Test

  • Comments - Quick Test

    10 questions on Java's three comment styles and how the compiler treats them.

    10 questions · 10 min · Easy
    Start Mock Test

Coding Problems