Home  >  Article  >  Java  >  How to solve Java compile-time errors and run-time errors

How to solve Java compile-time errors and run-time errors

王林
王林forward
2023-04-28 08:19:061645browse

Java compile-time errors and run-time errors

1. To distinguish between compile-time errors and run-time errors, you should first understand what compilation is? What is a run?

First of all, let’s take a look at this picture:

How to solve Java compile-time errors and run-time errors

The compilation period is the process of handing over the java source code we wrote to the compiler for execution. , plays the role of translation. This process mainly checks the syntax of the java source code. If there are no syntax errors, the source code is compiled into a bytecode file (ie. class file ; Errors are checked

. If there are no logical errors, the function of the program is realized and the result is output.

2. The difference between memory allocation during compilation and runtime

.

① During compilation, it only generates some instructions

in the bytecode file of the program to control the allocation of memory when the program is running. It only

knows the size and storage of memory allocation position, does not perform specific allocation operations. ②The running period is the real allocation of memory , determine the size

of the memory allocated by the program, and the

location of these variables where the memory should be stored. . 3. How to distinguish compilation errors and running errors when developing java projects in eclipse?

①Compilation errors generally refer to grammatical errors or obvious logical errors.

For example: missing semicolons, missing brackets, incorrect keyword writing, etc., red lines are often drawn in eclipse.

②Running errors are logical errors generated after running without compilation errors.

For example: Null pointer exception, divisor is 0, out-of-bounds access, etc., exceptions will generally be thrown.

4. Example

The following program, edit and run, the result is ()

public class Test{
           public void main(String[] args){
                      System.out.println("Hello world");
                      }
              }
Result:

Compilation without errors, But

an exception occurs when

is run.

The difference between compilation errors and running errorsCompilation period

Generate some instructions in the bytecode file of the program to control the allocation of memory when the program is running, just knowing the memory The allocated size and storage location do not perform specific allocation operations

Runtime

is the actual allocation of memory, which determines the size of the memory allocated by the program and where these variables should be stored in the memory Location

Common errors

Compilation errors generally refer to grammatical errors or obvious logical errors, such as: missing semicolons, missing parentheses, incorrect keyword writing, etc.

Runtime errors are logical errors that occur after running without compilation errors, such as: null pointer exception, division by 0, out-of-bounds access, etc.

The above is the detailed content of How to solve Java compile-time errors and run-time errors. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete