运行时和编译时的区别是什么?


当前回答

(编辑:以下内容适用于c#和类似的强类型编程语言。我不确定这是否对你有帮助)。

例如,在运行程序之前,编译器(在编译时)将检测到以下错误,并将导致编译错误:

int i = "string"; --> error at compile-time

另一方面,像下面这样的错误不能被编译器检测到。您将在运行时(当程序运行时)收到一个错误/异常。

Hashtable ht = new Hashtable();
ht.Add("key", "string");
// the compiler does not know what is stored in the hashtable
// under the key "key"
int i = (int)ht["key"];  // --> exception at run-time

其他回答

我们可以将其分为两大类静态绑定和动态绑定。它取决于何时与相应值绑定。如果引用是在编译时解析的,那么它是静态绑定;如果引用是在运行时解析的,那么它是动态绑定。静态绑定和动态绑定又称早期绑定和后期绑定。有时也称为静态多态和动态多态。

约瑟夫Kulandai‏。

编译时间:您作为开发人员编译代码的时间段。

运行时间:用户运行你的软件的时间段。

你需要更明确的定义吗?

这里有一个非常简单的答案:

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)

用户可以打开并运行编译后的程序。当应用程序正在运行时,它被称为运行时。

术语“运行时”和“编译时”经常被程序员用来指代不同类型的错误。编译时错误是一种问题,例如语法错误或缺少文件引用,从而阻止程序成功编译。编译器产生编译时错误,并通常指出源代码的哪一行导致了问题。

如果一个程序的源代码已经被编译成一个可执行程序,那么它在程序运行时可能仍然有错误。例子包括不能工作的特性、意外的程序行为或程序崩溃。这些类型的问题被称为运行时错误,因为它们发生在运行时。

参考

作为其他答案的补充,以下是我对外行的解释:

您的源代码就像一艘船的蓝图。它定义了船应该如何制造。

如果你把你的蓝图交给造船厂,他们在建造船的时候发现了一个缺陷,他们会立即停止建造并向你报告,在船离开干船坞或接触水之前。这是一个编译时错误。这艘船甚至从未真正漂浮过,也没有使用过它的引擎。这个错误之所以被发现,是因为它甚至阻止了这艘船的制造。

当您的代码编译完成时,就像船完成了一样。建好了,可以出发了。当你执行你的代码时,就像在航行中让船下水一样。乘客上了船,引擎在运转船体在水面上,这是运行时间。如果你的船有致命的缺陷,在处女航时就沉没了(或者可能是一些额外的麻烦),那么它就遇到了运行错误。

(编辑:以下内容适用于c#和类似的强类型编程语言。我不确定这是否对你有帮助)。

例如,在运行程序之前,编译器(在编译时)将检测到以下错误,并将导致编译错误:

int i = "string"; --> error at compile-time

另一方面,像下面这样的错误不能被编译器检测到。您将在运行时(当程序运行时)收到一个错误/异常。

Hashtable ht = new Hashtable();
ht.Add("key", "string");
// the compiler does not know what is stored in the hashtable
// under the key "key"
int i = (int)ht["key"];  // --> exception at run-time