⏱️ 5 min read • Beginner Level • Lesson 8
Completed on . You can revise this lesson or continue to the next topic.
Comments in Java are used to explain code, improve readability, and make programs easier to maintain. Comments are ignored by the Java compiler and do not affect program execution.
Single-line comments are used for short explanations and begin with
//.
// This is a single-line comment
public class Demo {
public static void main(String[] args) {
System.out.println("Hello World"); // prints output
}
}
// on a line is treated as a comment and ignored by the compiler.
Multi-line comments are used for longer explanations and are written between
/* and */.
/*
This is a multi-line comment
It spans multiple lines
*/
public class Example {
public static void main(String[] args) {
System.out.println("Multi-line comment example");
}
}
Documentation comments are used to generate API documentation using the
javadoc tool.
/**
* This class demonstrates documentation comment.
* @author ProwessApps
* @version 1.0
*/
public class Demo {
/**
* Adds two numbers
*/
public int add(int a, int b) {
return a + b;
}
}
/** and are used to create professional API documentation.
//./* */.🧠 Test your understanding with a quick quiz
Topic: Comments | Language: Java
/** Example comment */System.out.println(10 /* comment */ + 20);🎉 Great job! Keep learning Java step by step.
Mark this lesson as completed to track your learning progress. To keep track across devices, please login and save your learning progress.
Discussion
Ask questions, share suggestions, or discuss this lesson.