Java Comments — Java Technical Interview Questions & Answers
Java Comments
Java Comments
- Comments are notes for humans. The compiler ignores them. Three styles: // one line, /* many lines */, /** javadoc */ for classes and methods.
- Write why a tricky decision exists, not every obvious line. Don’t leave huge commented-out code forever — delete it or use git.
- MCQ favourite: name all three styles. Nesting /* */ carelessly can break compilation.
- Let's take this on the board with one tiny Main class — no extra files. Java Comments — comments are ignored by the compiler; only sum print runs. Start from main, go line by line, and stop at each print. That output is the proof for Comments.
Technical Interview Tip: Name //, /* */, and /** */ in one breath.
Code Examples for Java Comments
Example
// Java Comments
public class Main {
public static void main(String[] args) {
// This is a single-line comment
int sum = 2 + 3; /* inline note */
System.out.println(sum);
}
}Java Comments — comments are ignored by the compiler; only sum print runs.
How to Explain Java Comments to an Interviewer
Does the compiler run comments?
No Ignored
Name the three comment styles.
// /* */ /** */
What is javadoc for?
Document classes and methods