Runtime vs Compile time

2018-12-31 13:48发布

Can anyone please give me a good understanding of whats the difference between run-time and compile-time?

25条回答
浅入江南
2楼-- · 2018-12-31 14:37

I think of it in terms of errors, and when they can be caught.

Compile time:

string my_value = Console.ReadLine();
int i = my_value;

A string value can't be assigned a variable of type int, so the compiler knows for sure at compile time that this code has a problem

Run time:

string my_value = Console.ReadLine();
int i = int.Parse(my_value);

Here the outcome depends on what string was returned by ReadLine(). Some values can be parsed to an int, others can't. This can only be determined at run time

查看更多
与君花间醉酒
3楼-- · 2018-12-31 14:37

Look into this example:

public class Test {

public static void main(String[] args) {
    int[] x=new int[-5];//compile time no error
    System.out.println(x.length);
}}

The above code is compiled successfully,there is no syntax error,it is perfectly valid. But at the run time it throws following error.

Exception in thread "main" java.lang.NegativeArraySizeException
    at Test.main(Test.java:5)

Like when in compile time certain cases has been checked,after that run time certain cases has been checked once the program satisfies all the condition you will get an output. Otherwise you will get compile time or run time error.

查看更多
低头抚发
4楼-- · 2018-12-31 14:39

Runtime vs Compile time

Runtime and compile time are programming terms that refer to different stages of software program development.

Compile-time is the instance where the code you entered is converted to executable while Run-time is the instance where the executable is running.

The terms "runtime" and "compile time" are often used by programmers to refer to different types of errors too.

Compile-time checking occurs during the compile time. Compile time errors are error occurred due to typing mistake, if we do not follow the proper syntax and semantics of any programming language then compile time errors are thrown by the compiler. They wont let your program to execute a single line until you remove all the syntax errors or until you debug the compile time errors.

The following are usual compile time errors:

Syntax errors

Typechecking errors

Compiler crashes (Rarely)

Run-time type checking happens during run time of programs. Runtime errors are the errors that are generated when the program is in running state. These types of errors will cause your program to behave unexpectedly or may even kill your program. They are often referred as Exceptions . The

following are some usual runtime errors:

Division by zero

Dereferencing a null pointer

Running out of memory

查看更多
回忆,回不去的记忆
5楼-- · 2018-12-31 14:41

Here is a quote from Daniel Liang, author of 'Introduction to JAVA programming', on the subject of compilation:

"A program written in a high-level language is called a source program or source code. Because a computer cannot execute a source program, a source program must be translated into machine code for execution. The translation can be done using another programming tool called an interpreter or a compiler." (Daniel Liang, "Introduction to JAVA programming", p8).

...He Continues...

"A compiler translates the entire source code into a machine-code file, and the machine-code file is then executed"

When we punch in high-level/human-readable code this is, at first, useless! It must be translated into a sequence of 'electronic happenings' in your tiny little CPU! The first step towards this is compilation.

Simply put: a compile-time error happens during this phase, while a run-time error occurs later.

Remember: Just because a program is compiled without error does not mean it will run without error.

A Run-time error will occur in the ready, running or waiting part of a programs life-cycle while a compile-time error will occur prior to the 'New' stage of the life cycle.

Example of a Compile-time error:

A Syntax Error - how can your code be compiled into machine level instructions if they are ambiguous?? Your code needs to conform 100% to the syntactical rules of the language otherwise it cannot be compiled into working machine code.

Example of a run-time error:

Running out of memory - A call to a recursive function for example might lead to stack overflow given a variable of a particular degree! How can this be anticipated by the compiler!? it cannot.

And that is the difference between a compile-time error and a run-time error

查看更多
牵手、夕阳
6楼-- · 2018-12-31 14:42

here's a very simple answer:

Runtime and compile time are programming terms that refer to different stages of software program development. In order to create a program, a developer first writes source code, which defines how the program will function. Small programs may only contain a few hundred lines of source code, while large programs may contain hundreds of thousands of lines of source code. The source code must be compiled into machine code in order to become and executable program. This compilation process is referred to as compile time.(think of a compiler as a translator)

A compiled program can be opened and run by a user. When an application is running, it is called runtime.

The terms "runtime" and "compile time" are often used by programmers to refer to different types of errors. A compile time error is a problem such as a syntax error or missing file reference that prevents the program from successfully compiling. The compiler produces compile time errors and usually indicates what line of the source code is causing the problem.

If a program's source code has already been compiled into an executable program, it may still have bugs that occur while the program is running. Examples include features that don't work, unexpected program behavior, or program crashes. These types of problems are called runtime errors since they occur at runtime.

The reference

查看更多
公子世无双
7楼-- · 2018-12-31 14:43

As an add-on to the other answers, here's how I'd explain it to a layman:

Your source code is like the blueprint of a ship. It defines how the ship should be made.

If you hand off your blueprint to the shipyard, and they find a defect while building the ship, they'll stop building and report it to you immediately, before the ship has ever left the drydock or touched water. This is a compile-time error. The ship was never even actually floating or using its engines. The error was found because it prevented the ship even being made.

When your code compiles, it's like the ship being completed. Built and ready to go. When you execute your code, that's like launching the ship on a voyage. The passengers are boarded, the engines are running and the hull is on the water, so this is runtime. If your ship has a fatal flaw that sinks it on its maiden voyage (or maybe some voyage after for extra headaches) then it suffered a runtime error.

查看更多
登录 后发表回答