Java Comments
Explain the why behind the code.
Read this topic in தமிழ் ↗.
By the end of this lesson, you can…
- Explain the key idea behind Comments.
- Read and run a complete example, then predict what changes.
- Recognize the topic-specific mistake discussed below.
Understand the idea
Comments are notes for readers; they do not produce output. Use them to explain decisions or constraints rather than repeat what a clear statement already says. A useful comment stays true when the code changes.
Explore the details
Java Comments
Comments can be used to explain Java code, and to make it more readable. It can also be used to prevent execution when testing alternative code.
Single-line Comments
Single-line comments start with two forward slashes (//).
Any text between // and the end of the line
is ignored by Java (will not be executed).
This example uses a single-line comment before a line of code:
This example uses a single-line comment at the end of a line of code:
Java Multi-line Comments
Multi-line comments start with /* and ends with */.
Any text between /* and */ will be ignored by Java.
This example uses a multi-line comment (a comment block) to explain the code:
Single or multi-line comments?
It's up to you which one you use. Normally, we use // for short comments, and /* */ for longer.
// Single-line comment: ignored by Java
int x = 10; // x stores the value 10
/* Multi-line comment:
Explain multiple lines here.
Nothing inside runs. */
System.out.println(x); // 10
// Tip: use comments to disable code temporarily
// System.out.println("This line is skipped");A complete runnable example
This complete Main.java program focuses on the lesson idea. Reference fragments above may demonstrate individual statements; this example includes the imports, entry point, and supporting types it needs.
public class Main {
public static void main(String[] args) throws Exception {
// A welcome message for a new learner
System.out.println("Welcome!");
/* This comment can span
more than one line. */
System.out.println("Ready to practice.");
}
}Predict first. Then reveal the expected output / வெளியீடு
Welcome! Ready to practice.
The output shown was checked by running this complete example. Trace the executed statements in order, then compare with your run.
Change it. Run it. Understand it.
Load this lesson example into the editor. Change one value or statement, predict the result, then press RUN to execute Java.
Powered by OneCompiler. Code you run is sent to OneCompiler for execution. For Scanner examples, supply input in the editor I/O panel; for file examples, files exist in its execution environment.
Make it your own
Practice challenge
Comment out the second println without deleting it.
Reveal the answer and reasoning / விடை
Add // before that statement. Only Welcome! will be printed.
One mistake to avoid: Block comments cannot be nested. An early */ closes the current block comment.
Your learning checkpoint
- Explain the example without reading the output first.
- Make the change requested in the practice task.
- Describe why the result changed—or why compilation failed.
Ready to move on? Follow the next lesson in course order, or return to the curriculum to review a prerequisite.