Comments in Java
Comments are non-executable lines in your code. They help you and other developers understand the code easily.
Java ignores comments at runtime.
Types of Comments in Java
1. Single-Line Comment
- Used for short notes or explanations.
- Starts with
//.
int age = 25; // age of the person
2. Multi-Line Comment
- Used for longer explanations or block comments.
- Starts with
/*and ends with/.
/*
This program calculates the total salary
after adding bonus and tax deductions
*/
int salary = 50000;
3. Documentation Comment (Javadoc)
- Used to generate HTML documentation automatically.
- Starts with
/**and ends with/. - Typically placed above classes, methods, or variables.
/**
* This method calculates the sum of two numbers
* @param a first number
* @param b second number
* @return sum of a and b
*/
public int sum(int a, int b) {
return a + b;
}
Tips: Always use Javadoc for public classes and methods in projects.
Code Formatting Best Practices
Proper code formatting improves readability, maintainability, and reduces errors.
1. Indentation
- Use 4 spaces per level (do not mix tabs and spaces).
- Keep blocks aligned with braces
{}.
public class Example {
public static void main(String[] args) {
int a = 10;
if (a > 5) {
System.out.println("a is greater than 5");
}
}
}
2. Braces {} Style
Always use braces even for single statements to avoid mistakes.
// Good
if (a > 0) {
System.out.println(a);
}
// Bad
if (a > 0)
System.out.println(a);
3. Naming Conventions
| Type | Convention | Example |
|---|---|---|
| Class | PascalCase | StudentDetails |
| Method | camelCase | calculateSalary() |
| Variable | camelCase | totalSalary |
| Constant | UPPERCASE | MAX_AGE |
Consistent naming improves readability and helps in collaboration.
4. Spacing
- Add spaces around operators (
=,+, , etc.) - Avoid excessive spaces.
int sum = a + b; // Good
int sum=a+b; // Bad
5. Line Length
- Keep each line < 100 characters (optional 120 max).
- Break long statements for readability.
System.out.println("This is a long line that should be split into multiple lines for better readability.");
6. Comments Placement
Place comments above the code, not at the end of complex lines.
// Calculate the total price including tax
double totalPrice = price + (price * taxRate);
7. Avoid Hardcoding
Use constants or variables instead of magic numbers.
final double TAX_RATE = 0.05;
double totalPrice = price + (price * TAX_RATE);
8. Consistent Project Structure
- Group related classes and packages logically.
- Keep separate folders for source code, resources, and tests.
src/
main/
java/
com/example/project/
test/
java/
com/example/project/
Example – Cleanly Formatted Java Program
/**
* Program to calculate total salary after bonus and tax
*/
public class SalaryCalculator {
public static void main(String[] args) {
final double BONUS_RATE = 0.10;
final double TAX_RATE = 0.05;
double basicSalary = 50000;
// Calculate bonus
double bonus = basicSalary * BONUS_RATE;
// Calculate tax
double tax = basicSalary * TAX_RATE;
// Calculate total salary
double totalSalary = basicSalary + bonus - tax;
System.out.println("Total Salary: " + totalSalary);
}
}
