JAVA Comments
- Java comments are non-executable statements used to explain code, improve readability, or disable parts of the code during testing. The compiler ignores comments.
- Comments written by the programmer will be ignored by the machine during compilation and execution.
- Comments are written by programming for better understanding of the code.
Types Of Java Comments:
1. Single-line Comments: Start with // and extend to the end of the line.
int a = 5; // Variable declaration
Syntax:
// This is a single-line commentint a = 5; // Variable declaration
Example:
public class Main {public static void main(String[] args) {
// This prints a message to the console
System.out.println("Hello, World!");
}
}
2. Multi-line Comments: Enclosed between /* and */.
Syntax:
/* This is a multi-line comment.It can span multiple lines. */
int b = 10;
Example:
public class Main {public static void main(String[] args) {
/*
This block explains the purpose of the program.
The program calculates the sum of two numbers.
*/
int a = 5;
int b = 10;
int sum = a + b;
System.out.println("Sum: " + sum);
}
}