Compile-time & Runtime Errors in Java..

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:

  • Syntax checking occurs at this stage.
  • Errors such as syntax errors, missing semicolons, incorrect data types, undeclared variables, etc., are identified.
  • The program won't run if compile-time errors exist.

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

  1. ArithmeticException: Issues like division by zero.

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 -

要查看或添加评论,请登录

Arjun K.的更多文章

  • Java OOPs...Encapsulation!

    Java OOPs...Encapsulation!

    Consider a real-life example of your bank account, your account balance is private—you wouldn’t want anyone to directly…

  • Little’s Law in Performance Testing

    Little’s Law in Performance Testing

    In 1954, John Little published a paper in which he described the queueing theory by a new law. Later, it is named…

  • Performance Metrics- Throughput, latency and response time!

    Performance Metrics- Throughput, latency and response time!

    Throughput Throughput serves as a crucial performance metric for assessing system efficiency, identifying bottlenecks…

  • Application Performance Improvement using CAST SQG formerly AIP.

    Application Performance Improvement using CAST SQG formerly AIP.

    ??What is CAST Structural Quality Gate (SQG) formerly AIP ? CAST SQG draws on CAST’s extensive experience in deep…

  • Performance Test-An Overview!

    Performance Test-An Overview!

    Performance testing is a type of software testing that focuses on how well an application performs under various…

  • Software Performance Test - An Overview!

    Software Performance Test - An Overview!

    Performance testing is a type of software testing that focuses on how well an application performs under various…

  • Java for Everyone...Encapsulation.

    Java for Everyone...Encapsulation.

    Consider a real-life example of your bank account. Your account balance is private—you wouldn’t want anyone to directly…

  • Java Collection Framework in Selenium

    Java Collection Framework in Selenium

    In Selenium WebDriver, Java collections play a crucial role in managing and manipulating data such as lists of web…

  • Java for Everyone... Arrays

    Java for Everyone... Arrays

    An array is a container object that holds a fixed number of values of a single type. The length of an array is…

  • Java for Everyone... StringBuilder & StringBuffer

    Java for Everyone... StringBuilder & StringBuffer

    StringBuilder objects are like String objects, except that they can be modified. Internally, these objects are treated…

社区洞察

其他会员也浏览了