Single-Line Comments
A free lesson from Swipe Coding · Java, level 2 · updated October 2026
By the Swipe team, founded by Colin · how we make lessons
The simplest way to write a comment in Java is with two forward slashes: //. Everything on the same line after // is completely ignored by the compiler.
public class Main {
public static void main(String[] args) {
// Prints a greeting
System.out.println("Hello!");
}
}A single-line comment doesn't have to be on its own line. It can also appear directly after code on the same line — this is called an "inline comment."
System.out.println("Hello!"); // prints "Hello!" to the screenA single-line comment in Java begins with the characters ____.
With //, you can also temporarily "turn off" an entire line of code without deleting it. This is called "commenting out." Java then treats the line as if it weren't there at all.
public class Main {
public static void main(String[] args) {
System.out.println("Line 1");
// System.out.println("Line 2");
System.out.println("Line 3");
}
}What does this program print?
System.out.println("A");
// System.out.println("B");
System.out.println("C");Important: // only comments out text up to the end of the current line. A line break automatically ends the comment — the next line is read as normal code again.
The // comment automatically also applies to the next line of code.
Keep your streak going in Swipe Coding
Swipe through every level of Java on your iPhone — with XP, stars, streaks, a personal advisor and the full track beyond these free levels.
Get the Swipe apps →