Nearby lessons

9 of 125

Java - Comments and Javadoc

📌 What You Will Learn
  • How to set up Java (PATH) on your computer
  • How to write, save, compile and run your first program
  • The meaning of every word in the main method
  • The rules about class name and file name
  • How to fix the common errors a beginner faces

Comments and Javadoc is a core concept of the Java language. This lesson explains Comments in Java and Comments and the javadoc Tool with complete, runnable code examples, clear step-by-step explanations, and common mistakes to avoid with exam-style MCQs at the end.

Comments in Java

Comments are notes written for humans (yourself or teammates). The compiler completely ignores them. There are three types:

Example01
JCode Cell
1// 1. Single line comment - starts with two slashes
2 
3/* 2. Multi line comment - starts with slash-star
4 and ends with star-slash */
5 
6/** 3. Documentation comment - used with javadoc tool
7 to create HTML help documents */

Comments in Java

Example02
JCode Cell
1class CommentDemo {
2 public static void main(String[] args) {
3 // this line prints a message
4 System.out.println("Comments are ignored"); /* so this works */
5 }
6}

Comments and the javadoc Tool

We already saw the three comment types in Chapter 2. The special documentation comment (/** ... */) is used with the javadoc tool, which automatically creates HTML help documents (API documentation) for your classes.

On the command prompt, run:

Example03
JCode Cell
1/**
2 * This class represents an Employee.
3 * @author Rahul
4 * @version 1.0
5 */
6class Employee { }

Comments and the javadoc Tool

This creates a set of .html files — the same style of documentation pages you see on the official Java website.

Example04
JCode Cell
1C:\> javadoc Employee.java
📝 Key Takeaways
  • Six steps: install JDK → choose editor → write program → save file → compile with javac → run with java.
  • File name must match the public class name exactly, and must end with .java.
  • javac creates a .class file (bytecode); java runs it.
  • public static void main(String[] args) is the entry point of every program.
  • Comments (//, /* */, /** */) are ignored by the compiler and are used for documentation.
  • Most beginner errors are due to PATH, folder location, or spelling mistakes.

🧠 Test Your Knowledge

2 Questions
Progress: 0 / 2