运行时和编译时的区别是什么?
当前回答
看看这个例子:
public class Test {
public static void main(String[] args) {
int[] x=new int[-5];//compile time no error
System.out.println(x.length);
}}
上面的代码编译成功,没有语法错误,完全有效。 但是在运行时,它抛出以下错误。
Exception in thread "main" java.lang.NegativeArraySizeException
at Test.main(Test.java:5)
比如在编译时检查了某些情况,在运行时检查了某些情况,一旦程序满足所有条件,就会得到输出。 否则,您将得到编译时或运行时错误。
其他回答
看看这个例子:
public class Test {
public static void main(String[] args) {
int[] x=new int[-5];//compile time no error
System.out.println(x.length);
}}
上面的代码编译成功,没有语法错误,完全有效。 但是在运行时,它抛出以下错误。
Exception in thread "main" java.lang.NegativeArraySizeException
at Test.main(Test.java:5)
比如在编译时检查了某些情况,在运行时检查了某些情况,一旦程序满足所有条件,就会得到输出。 否则,您将得到编译时或运行时错误。
以下是前面类似的问题的答案,运行时错误和编译器错误的区别是什么?
编译/编译时/语法/语义错误:编译或编译时错误是由于键入错误而发生的错误,如果我们没有遵循任何编程语言的正确语法和语义,那么编译器就会抛出编译时错误。除非您删除所有语法错误或调试编译时错误,否则它们不会让您的程序执行一行。 例如:在C语言中缺少分号或将int错误地输入为int。
运行时错误:运行时错误是指程序处于运行状态时产生的错误。这些类型的错误将导致您的程序出乎意料地运行,甚至可能杀死您的程序。它们通常被称为例外。 示例:假设您正在读取一个不存在的文件,将导致运行时错误。
阅读更多关于所有编程错误
Imagine that you are a boss and you have an assistant and a maid, and you give them a list of tasks to do, the assistant (compile time) will grab this list and make a checkup to see if the tasks are understandable and that you didn't write in any awkward language or syntax, so he understands that you want to assign someone for a Job so he assign him for you and he understand that you want some coffee, so his role is over and the maid (run time)starts to run those tasks so she goes to make you some coffee but in sudden she doesn’t find any coffee to make so she stops making it or she acts differently and make you some tea (when the program acts differently because he found an error).
编制时间: 将源代码转换为机器码以使其成为可执行文件所花费的时间称为编译时间。
运行时间: 当应用程序正在运行时,它被称为运行时。
编译时错误是那些语法错误,缺少文件引用错误。 运行时错误发生在源代码被编译成可执行程序之后,并且程序正在运行时。例如程序崩溃、意外的程序行为或功能不工作。
这里是对“运行时和编译时的区别?”这个问题的回答的扩展。运行时和编译时开销的差异?
产品的运行时性能通过更快地交付结果来提高其质量。产品的编译时性能通过缩短编辑-编译-调试周期来提高其时效性。然而,运行时性能和编译时性能都是实现及时性质量的次要因素。因此,只有当整体产品质量和时效性得到改善时,才应该考虑运行时和编译时性能的改进。
这里有一个很好的进一步阅读的来源: