Author
Comments are non-executable lines in your code. They help you and other developers understand the code easily.
Java ignores comments at runtime.
//.int age = 25; // age of the person
/* and ends with /./*
This program calculates the total salary
after adding bonus and tax deductions
*/
int salary = 50000;
/** and ends with /./**
* 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.
Proper code formatting improves readability, maintainability, and reduces errors.
{}.public class Example {
public static void main(String[] args) {
int a = 10;
if (a > 5) {
System.out.println("a is greater than 5");
}
}
}
{} StyleAlways 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);
| Type | Convention | Example |
|---|---|---|
| Class | PascalCase | StudentDetails |
| Method | camelCase | calculateSalary() |
| Variable | camelCase | totalSalary |
| Constant | UPPERCASE | MAX_AGE |
Consistent naming improves readability and helps in collaboration.
=, +, , etc.)int sum = a + b; // Good
int sum=a+b; // Bad
System.out.println("This is a long line that should be split into multiple lines for better readability.");
Place comments above the code, not at the end of complex lines.
// Calculate the total price including tax
double totalPrice = price + (price * taxRate);
Use constants or variables instead of magic numbers.
final double TAX_RATE = 0.05;
double totalPrice = price + (price * TAX_RATE);
src/
main/
java/
com/example/project/
test/
java/
com/example/project/
/**
* 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);
}
}