Compile-time & Runtime Errors in Java..
Compile-time
A compile-time error in Java refers to an error that occurs during the compilation phase, when the Java compiler converts source code into bytecode. These errors prevent the program from compiling successfully, meaning the code cannot be executed until the issues are resolved.
Example
1. Syntax Errors: Violations of the language’s grammatical rules.
public static void main(String[] args) {
System.out.println("Hello Arjun")
}
2. Type Mismatch Errors: Assigning incompatible data types.
public static void main(String[] args) {
int x = "Hello"; // Incompatible types
}
Key Characteristics:
Runtime
A runtime error in Java refers to an error that occurs while the program is running after it has successfully compiled. These errors are caused by unexpected or invalid operations during execution, such as incorrect logic, invalid user input, or unhandled exceptional conditions.
Examples
领英推荐
public static void main(String[] args) {
int result = 10 / 0; // Division by zero
2. NullPointerException: Accessing a method or property of a null object.
public static void main(String[] args) {
int result = 10 / 0; // Division by zero
}
3. ArrayIndexOutOfBoundsException: Accessing invalid indices in an array.
public static void main(String[] args) {
int[] arr = {1, 2, 3};
System.out.println(arr[5]); // Index out of bounds
}
Key Differences
Compile-time Vs Runtime
Please refer below official page for more details on error , exception and its remediation -