Home› Java Programming Language› / Programs / Java Program to Demonstrate Single-Line Comments
🚀 Programming Example

Java Program to Demonstrate Single-Line Comments

5,035 Views
Java Java 8 or later
Beginner
Full Solution

Learn how to write a single-line comment in Java using //. This program declares an integer variable, adds a comment beside it to explain the code, and prints the variable's value. The comment doesn't change the output.

Before You Begin

What you will learn

  • Understand what a comment is and why programmers use it
  • Write a single-line comment with //
  • Place a comment on its own line or at the end of a line of code
  • Understand that the compiler ignores comments, so they don't affect the output
  • Declare an int variable and print its value

Prerequisites

  • JDK (Java Development Kit) installed on your computer
  • Basic structure of a Java program: a class and the main() method
  • How to print output with System.out.println()
  • Basic idea of variables and the int data type
  • How to compile with javac and run with java

📌 Information & Algorithm

Algorithm

  1. Start
  2. Create a public class named SingleLineComment.
  3. Define the main() method inside the class.
  4. Declare an integer variable i and assign it the value 10.
  5. Add a single-line comment after the statement to explain the variable. The compiler ignores this comment.
  6. Display the value of i using System.out.println().
  7. Stop

How to Run

Environment: JDK (Windows, Linux, macOS)

Execution command:

javac SingleLineComment.java
java SingleLineComment

💻 Program Code

public class SingleLineComment {
public static void main(String[] args) {
    int i=10;//it is comment it says Here, i is a variable
    System.out.println(i);
   }
}

Sample Input

No input required

🖥 Program Output

                                10
Press any key to continue . . .
                            

📘 Explanation

Overview

A comment is a note in the source code that explains what the code does. The Java compiler ignores comments completely, so they never change how the program runs. This program shows a single-line comment, which starts with //.

Everything after // up to the end of that line is a comment. The code before // on the same line still runs normally.

Step-by-Step Explanation

1

Class Declaration

The container for the program.

public class SingleLineComment {
    ...
}

The class is named SingleLineComment, so the file must be saved as SingleLineComment.java.

2

The main() Method

Where the program starts.

public static void main(String[] args)

The JVM starts running the program from this method.

3

Variable with a Single-Line Comment

Code and comment on the same line.

int i = 10; // This is a single-line comment: i is a variable

int i = 10; creates an integer variable named i and stores 10 in it. The text after // is the comment. The compiler skips it.

4

Print the Value

Shows the result on the screen.

System.out.println(i);

This prints the value stored in i, which is 10. There are no quotes around i, so Java prints the variable's value, not the letter "i".

Types of Comments in Java

Type Syntax Use
Single-line // comment Short notes on one line
Multi-line /* comment */ Longer notes across several lines
Documentation /** comment */ Used by the javadoc tool to create documentation

Where You Can Place a Single-Line Comment

// Comment on its own line
int i = 10;

int j = 20; // Comment at the end of a line of code

Output

10

Common Mistakes

Putting code after // // int i = 10; turns the whole statement into a comment, so i is never declared and System.out.println(i) gives a compile error.
Using a single slash / comment isn't a comment. Java reads it as a division operator and gives an error.
Writing comments that state the obvious A comment like // set i to 10 adds nothing. Explain why the code does something, not just what it does.
Good practice Use // to temporarily switch off a line of code while testing, then remove the // to switch it back on.

Key Takeaway

A single-line comment starts with // and runs to the end of the line. The compiler ignores it, so it helps people understand the code without changing the output.

Complexity

Time: O(1)

Space: O(1)

📚 Learning Subject

Master Programming Through Practical Examples

Improve your coding logic, problem-solving skills and programming confidence by practicing real-world examples with explanations.

🎯 How to learn from this example

First understand the algorithm carefully. Then study the program line-by-line and compare it with the output. Finally, review the explanation section to strengthen your logic and programming understanding.

🔥 Practice suggestion

Rewrite the program without looking at the code. Modify values, conditions or logic and run it again. This helps improve confidence and strengthens coding skills much faster.