1. Comments áááŻáᏠááŹáá˛?
Comments are text notes in your code that are ignored by the compiler. They are used to explain what the code does, making it easier for you and others to understand the program.
Comments áááŻáᏠcode áá˛áážáŹ ááąá¸ááŹá¸áá˛áˇ áážááşááťááşáá˝áą áááŻáˇáááŻááş áážááşá¸áááşá¸ááťááşáá˝áą ááźá áşááŤáááşá Compiler á comments áá˝áąááᯠááŻáśá¸á ááťá áşááťá°áážáŻááŤáááş - compile ááŻááşáá˛áˇ áĄá፠skip ááŻááşáá˝áŹá¸ááŤáááşá
ááŹááźáąáŹááˇáş áááŻáĄááşáá˛?
- Code ááᯠááŹá¸áááşá áá˝ááşá áąáááş: Complex logic áá˝áąááᯠáážááşá¸ááźáááŻááşáááş
- Documentation: Function/Method á ááŹááŻááşááŹáᲠáážááşáááşá¸áááşáááŻááşáááş
- Team collaboration: áĄááźáŹá¸ developers áá˝áą code ááŹá¸áááşáĄáąáŹááş áá°ááŽááąá¸áááş
- Debugging: Code áĄáááŻááşá¸áá áşááŻááᯠááŹáᎠááááşááŹá¸áááŻáˇ ááááş (comment out)
- Reminders: TODO, FIXME á áá˛áˇ áážááşááŹá¸ááááˇáş áĄááŹáá˝áą áážááşááŹá¸áááŻáˇ ááááş
Key Point: Comments á program áá˛áˇ behavior ááᯠáááźáąáŹááşá¸ááŤá Compiler á ignore ááŻááşáááŻáˇ output áážáŹ áááşááąáŹááşáážáŻ ááážáááŤá
Single-Line
Short comments on one line using //
Multi-Line
Longer comments spanning multiple lines using /* */
Documentation
Special comments for API docs using /** */
2. Single-Line Comments (áá áşááźáąáŹááşá¸áááşá¸ áážááşááťááş)
Single-line comments start with // and continue to the end of the line. Everything
after // is ignored by the compiler.
public class SingleLineExample {
public static void main(String[] args) {
// This is a single-line comment
System.out.println("Hello, World!");
// Print the current year
System.out.println("Year: 2024");
int age = 25; // Variable to store age
// Calculate next year's age
int nextAge = age + 1;
System.out.println("Next year: " + nextAge); // Output: 26
}
}
Hello, World!
Year: 2024
Next year: 26
Syntax: // áá˛áˇ á
ááŤáááşá Double forward slashes ááŻáśá¸ááŤáááşá
Usage patterns:
- Above the code: Code áĄááąáŤáşáážáŹ comment áááˇáşááźáŽá¸ ááŹááŻááşáááş áááŻááŹ
áážááşá¸ááźááŤ
// Calculate total price int total = price * quantity; - End of line: Code áá˛áˇ áĄááŻáśá¸áážáŹ comment áááˇáşááźáŽá¸ áážááşá¸áááşá¸ááťááş ááąá¸ááŤ
int age = 18; // Legal age for voting - Standalone line: ááŽá¸ááźáŹá¸ line áá
áşááźáąáŹááşá¸ááŻáśá¸ comment áĄááźá
áş ááŻáśá¸ááŤ
// This section handles user authentication
áááşáĄááťáááşáážáŹ ááŻáśá¸áááˇáşáá˛?
- áááŻááąáŹááşá¸áá˛áˇ áážááşá¸áááşá¸ááťááşáá˝áą áĄáá˝ááş
- Variable/constant áá˛áˇ purpose áážááşá¸ááźááťááşáá˛áˇ áĄááŤ
- Code áá áşááźáąáŹááşá¸ áááŻáˇáááŻááş áážá áşááźáąáŹááşá¸ áĄááźáąáŹááşá¸ áážááşá¸ááźááťááşáá˛áˇ áĄááŤ
- TODO, FIXME markers áááˇáşááťááşáá˛áˇ áĄááŤ
đ áĄááąá¸ááźáŽá¸áá˛áˇ á
ááşá¸ááťááşá¸: // ááąáŹááşá áĄááŹáĄáŹá¸ááŻáśá¸ comment
ááźá
áşááŤáááş (same line áážáŹ)á Next line á ááŻáśáážááş code ááźááşááźá
áşááŤáááşá
Commenting Out Code (Code ááᯠááŹáᎠááááşááŹá¸ááźááşá¸)
public class DebugExample {
public static void main(String[] args) {
System.out.println("Program started");
// Temporarily disabled for testing
// System.out.println("Debug info");
// int debugValue = 100;
System.out.println("Program ended");
}
}
Debugging: Code áĄáááŻááşá¸áá áşááŻááᯠááŹáᎠááááşááźáŽá¸ ááźáżáᏠáážáŹááťááşáá˛áˇ áĄá፠ááŻáśá¸ááŤáááşá Code ááᯠdelete áááŻááşáᲠcomment out ááŻááşááŹá¸áááŻáˇ ááąáŹááşááž ááźááşáá˝ááˇáşáááŻáˇ áááŤáááşá
Testing alternatives: Code áá˛áˇ different versions áá˝áąááᯠá ááşá¸ááťááşáá˛áˇ áĄá፠old code ááᯠcomment out ááŻááşááźáŽá¸ new code ááąá¸áááŻááşááŤáááşá
â ď¸ Warning: Commented code ááᯠproduction áážáŹ áááŹá¸áááˇáşááŤá Debugging ááźáŽá¸áááş ááťááşáá áşááŤá
3. Multi-Line Comments (ááťáŹá¸ááźáąáŹááşá¸ áážááşááťááş)
Multi-line comments start with /* and end with */. Everything between
these markers is ignored by the compiler, even if it spans multiple lines.
public class MultiLineExample {
public static void main(String[] args) {
/*
* This is a multi-line comment
* It can span multiple lines
* Compiler ignores everything between /* and */
*/
System.out.println("Hello!");
/*
Calculate the area of a rectangle
Formula: length * width
Author: Your Name
Date: 2024-01-15
*/
int length = 10;
int width = 5;
int area = length * width;
System.out.println("Area: " + area);
/* This can also be on a single line */
int x = 100;
}
}
Hello!
Area: 50
Syntax:
- Start:
/* - End:
*/ - áĄááźáŹá¸á áĄááŹáĄáŹá¸ááŻáśá¸ comment ááźá áşááŤáááş
Common formatting styles:
/* Simple style */
/*
* Asterisk-aligned style
* Second line
* Third line
*/
/*
Indented style
Second line
Third line
*/
áááşáĄááťáááşáážáŹ ááŻáśá¸áááˇáşáá˛?
- Long descriptions: Complex logic ááᯠáĄááąá¸á áááş áážááşá¸ááźááťááşáá˛áˇ áĄááŤ
- File headers: File information, copyright, author details áá˝áą áááˇáşáá˛áˇ áĄááŤ
- Algorithm explanations: Step-by-step process áá˝áą document ááŻááşáá˛áˇ áĄááŤ
- Temporary disabling: Code block ááźáŽá¸ááᯠááŹáᎠááááşááŹá¸ááťááşáá˛áˇ áĄááŤ
â ď¸ Warning: Multi-line comments áá˝áąá nested (áá áşááŻáĄáá˝ááşá¸áážáŹ áá áşááŻ) ááŻááşáááŻáˇ ááááŤ!
/* Outer comment
/* Inner comment - THIS WILL CAUSE ERROR! */
*/
Commenting Out Code Blocks
public class Example {
public static void main(String[] args) {
System.out.println("Active code");
/*
// Disabled feature
int oldCalculation = 10 * 20;
System.out.println("Old way: " + oldCalculation);
// More disabled code
for (int i = 0; i < 5; i++) {
System.out.println(i);
}
*/
// New implementation
int newCalculation = 15 * 20;
System.out.println("New way: " + newCalculation);
}
}
Multi-line comments á code block ááťáŹá¸ááťáŹá¸ ááááşááťááşáá˛áˇ áĄá፠áĄáááşááźáąááŤáááşá Single-line comments
áá˛áˇ ááááşáááş line áá
áşááźáąáŹááşá¸ááťááşá¸á
Ꭰ// áááˇáşááááŻáˇ áááŻáááşáááşá¸ááŤáááşá
Multi-line comments áĄáá˝ááşá¸áážáŹ single-line comments áá˝áą ááŤáááŻááşááŤáááşá ááŤááąáááˇáş multi-line comments nested áááŻááşáááŻááşáᏠáááááŹá¸ááŤá
4. Documentation Comments (Javadoc)
Documentation comments are special comments that start with /** and end with
*/. They are used to generate API documentation automatically using the
javadoc tool.
/**
* This class demonstrates Javadoc comments.
* It provides methods for basic arithmetic operations.
*
* @author Your Name
* @version 1.0
* @since 2024-01-15
*/
public class Calculator {
/**
* Adds two integers and returns the result.
*
* @param a the first integer
* @param b the second integer
* @return the sum of a and b
*/
public int add(int a, int b) {
return a + b;
}
/**
* Subtracts the second integer from the first.
*
* @param a the first integer (minuend)
* @param b the second integer (subtrahend)
* @return the difference of a and b
* @throws IllegalArgumentException if b is greater than a
*/
public int subtract(int a, int b) {
if (b > a) {
throw new IllegalArgumentException("b cannot be greater than a");
}
return a - b;
}
/**
* Calculates the area of a rectangle.
*
* This method multiplies length and width to get the area.
* Both parameters must be positive.
*
* @param length the length of the rectangle
* @param width the width of the rectangle
* @return the area of the rectangle
* @see #calculatePerimeter(int, int)
*/
public int calculateArea(int length, int width) {
return length * width;
}
/**
* Calculates the perimeter of a rectangle.
*
* @param length the length of the rectangle
* @param width the width of the rectangle
* @return the perimeter of the rectangle
* @see #calculateArea(int, int)
*/
public int calculatePerimeter(int length, int width) {
return 2 * (length + width);
}
}
Syntax: /** áá˛áˇ start ááŻááşááźáŽá¸ */ áá˛áˇ end
ááźá
áşááŤáááşá Multi-line comment áá˛áˇ áááşáááşá ááŤááąáááˇáş extra asterisk (*) áá
áşáᯠáááŻááŤáááşá
Common Javadoc Tags:
@author- Code ááąá¸áá° áĄáááş@version- Version number@since- áááş version/date áážáŹ á áááş áááˇáşáá˝ááşá¸áá˛áˇáá˛@param- Method parameter áážááşá¸áááşá¸ááťááş@return- Return value áážááşá¸áááşá¸ááťááş@throws/@exception- Exception áá˝áą ááźá áşáááŻááşáᏠáážááşá¸ááźáááş@see- Related methods/classes áá˝áą áá˝ážááşá¸ááźáááş@deprecated- áĄá˛áˇáᎠmethod/class á outdated ááźá áşááźáŽáááŻáᏠmark ááŻááşáááş
ááŹááźáąáŹááˇáş áĄááąá¸ááźáŽá¸áá˛?
- Professional documentation: javadoc tool á HTML documentation ááᯠautomatically generate ááŻááşááąá¸ááŤáááş
- IDE support: Modern IDEs áá˝áąá Javadoc ááᯠáááşááźáŽá¸ tooltips, autocomplete áážáŹ information ááźááŤáááş
- API documentation: Library developers áá˝áą ááŻáśá¸áá˛áˇ standard format ááŤ
- Team collaboration: Method/class á ááŹááŻááşáááşáááŻáᏠááźááşááźááşááŹá¸ááŹá¸ ááá áąááŤáááş
Generating documentation:
javadoc Calculator.java
ááŤá HTML files áá˝áą generate ááŻááşááąá¸áážáŹááŤá Browser áá˛áˇ áá˝ááˇáşááźááˇáşáááŻáˇ áááŤáááşá
Class-Level vs Method-Level Javadoc
/**
* Represents a student in the school system.
* This class stores student information and provides
* methods to manage student data.
*
* @author School Admin
* @version 2.0
*/
public class Student {
/** Student's unique ID number */
private int studentId;
/** Student's full name */
private String name;
/**
* Creates a new Student with the given ID and name.
*
* @param studentId unique identifier for the student
* @param name full name of the student
*/
public Student(int studentId, String name) {
this.studentId = studentId;
this.name = name;
}
/**
* Gets the student's ID.
*
* @return the student's unique ID number
*/
public int getStudentId() {
return studentId;
}
/**
* Gets the student's name.
*
* @return the student's full name
*/
public String getName() {
return name;
}
}
- Class level: Class áá˛áˇ purpose, version, author áá˝áą document ááŻááşááŤ
- Method level: Method áá˛áˇ functionality, parameters, return value, exceptions áá˝áą document ááŻááşááŤ
- Field level: Important instance variables áá˝áąááᯠáážááşá¸ááźááŤ
(áááŻááąáŹááşá¸áááş
/** */one-liner ááŻáśá¸áááŻáˇ áááŤáááş) - Constructor level: Constructor parameters áá˝áąáá˛áˇ initialization logic ááᯠáážááşá¸ááźááŤ
đ Public APIs, libraries, frameworks áá˝áąáážáŹ Javadoc á áááźá áşáááą áááˇáşáááˇáşááŤáááşá
5. Comment Best Practices (áĄááąáŹááşá¸ááŻáśá¸ áĄááąáˇáĄááťááˇáşááťáŹá¸)
â Good Comment Practices
1. Explain WHY, not WHAT
// â Bad: Explains what (obvious from code)
int age = 25; // Set age to 25
// â
Good: Explains why
int age = 25; // Minimum age required for insurance eligibility
2. Keep comments updated
// â Bad: Outdated comment
// Calculate discount as 10%
double discount = price * 0.20; // Actually 20% now!
// â
Good: Accurate comment
// Calculate discount as 20% for premium members
double discount = price * 0.20;
3. Use comments for complex logic
// â
Good: Explains complex algorithm
// Binary search algorithm - O(log n) time complexity
// Divides search space in half each iteration
int mid = (low + high) / 2;
4. Add TODO and FIXME markers
// TODO: Implement input validation
// FIXME: Bug when input is negative
// NOTE: This method is performance-critical
// HACK: Temporary workaround until API is fixed
5. Comment public APIs
/**
* â
Good: Complete documentation for public method
* Validates user email format.
*
* @param email the email address to validate
* @return true if valid, false otherwise
*/
public boolean validateEmail(String email) {
// Implementation
return email.contains("@");
}
â ď¸ Bad Comment Practices (áážáąáŹááşááźááşáááˇáşááŹááťáŹá¸)
1. Stating the obvious
// â Bad: Obvious from code
int x = 10; // Assign 10 to x
x++; // Increment x by 1
2. Commented-out code without explanation
// â Bad: No context
// int oldValue = 100;
// System.out.println(oldValue);
// â
Better: Explain why commented
// Old implementation - removed after performance testing
// Keeping for reference until version 2.0 release
// int oldValue = 100;
3. Redundant comments
// â Bad: Comment repeats method name
/**
* Gets the name.
* @return the name
*/
public String getName() {
return name;
}
// â
Better: Add value
/**
* Returns the user's full name including middle initial if available.
* @return formatted full name
*/
public String getName() {
return name;
}
4. Excessive comments
// â Bad: Too many obvious comments
public void calculateTotal() {
// Declare variable for price
int price = 100;
// Declare variable for quantity
int quantity = 5;
// Multiply price and quantity
int total = price * quantity;
// Print the total
System.out.println(total);
}
// â
Better: One meaningful comment
public void calculateTotal() {
// Calculate order total with current pricing
int total = 100 * 5;
System.out.println(total);
}
- Write self-documenting code first: Variable/method names ááᯠáážááşá¸áážááşá¸áááşá¸áááşá¸ ááąá¸ááŤá Comment ááááŻáĄáąáŹááş code á clear ááźá áşáááˇáşááŤáááş
- Comment the "why", not the "what": Code á "what" ááᯠááźááąááźáŽá¸ááŹá¸á Comment á "why" ááᯠáážááşá¸ááźááŤ
- Keep comments concise: áááŻááąáŹááşá¸ááźáŽá¸ áááąáŹááąáŤááşáá˝ááşáĄáąáŹááş ááąá¸ááŤ
- Update comments when code changes: Code ááźáąáŹááşá¸áááş comment áááŻáááşá¸ update ááŻááşááŤ
- Remove unused commented code: Production áážáŹ commented code áááŹá¸ááŤáá˛áˇ
- Use Javadoc for public APIs: Public methods/classes áá˝áąáážáŹ Javadoc ááŻáśá¸ááŤ
6. Key Takeaways (áážááşááŹá¸ááááˇáş áĄááťááşááťáŹá¸)
- Single-line comments:
//- Short, one-line explanations - Multi-line comments:
/* */- Longer explanations spanning multiple lines - Documentation comments:
/** */- Javadoc for generating API documentation - Comments are ignored by compiler - No effect on program execution
- Explain WHY, not WHAT - Code shows what, comments explain why
- Keep comments updated - Outdated comments are worse than no comments
- Use Javadoc for public APIs - Essential for libraries and frameworks
- Avoid obvious comments - Don't state what's clear from code
- Use TODO/FIXME markers - Track work that needs to be done
- Self-documenting code is best - Clear names reduce need for comments
đĄ Practice Exercise
Task: Add appropriate comments to this code:
public class Circle {
private double radius;
public Circle(double radius) {
this.radius = radius;
}
public double calculateArea() {
return 3.14159 * radius * radius;
}
public double calculateCircumference() {
return 2 * 3.14159 * radius;
}
}
Add:
- Class-level Javadoc comment
- Constructor Javadoc comment
- Method Javadoc comments with @param and @return tags
- Inline comment explaining the formulas
đĄ Quick Quiz
Q1: Which comment type is used for generating API documentation?
Q2: Are comments compiled into bytecode?
Q3: What should comments primarily explain?