运行时和编译时的区别是什么?
当前回答
看看这个例子:
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 RuntimeVsCompileTime {
public static void main(String[] args) {
//test(new D()); COMPILETIME ERROR
/**
* Compiler knows that B is not an instance of A
*/
test(new B());
}
/**
* compiler has no hint whether the actual type is A, B or C
* C c = (C)a; will be checked during runtime
* @param a
*/
public static void test(A a) {
C c = (C)a;//RUNTIME ERROR
}
}
class A{
}
class B extends A{
}
class C extends A{
}
class D{
}
以下是前面类似的问题的答案,运行时错误和编译器错误的区别是什么?
编译/编译时/语法/语义错误:编译或编译时错误是由于键入错误而发生的错误,如果我们没有遵循任何编程语言的正确语法和语义,那么编译器就会抛出编译时错误。除非您删除所有语法错误或调试编译时错误,否则它们不会让您的程序执行一行。 例如:在C语言中缺少分号或将int错误地输入为int。
运行时错误:运行时错误是指程序处于运行状态时产生的错误。这些类型的错误将导致您的程序出乎意料地运行,甚至可能杀死您的程序。它们通常被称为例外。 示例:假设您正在读取一个不存在的文件,将导致运行时错误。
阅读更多关于所有编程错误
编译时间:您作为开发人员编译代码的时间段。
运行时间:用户运行你的软件的时间段。
你需要更明确的定义吗?
对于S.O.来说,这不是一个好问题(这不是一个特定的编程问题),但总的来说,这不是一个坏问题。
如果您认为这是微不足道的:那么读时与编译时的区别是什么呢?什么时候这是一个有用的区别?编译器在运行时可用的语言呢?Guy Steele(不是笨蛋,他)在CLTL2中写了7页关于EVAL-WHEN的内容,CL程序员可以使用它来控制这一点。两句话只能勉强给出一个定义,而定义本身还远远不够解释。
In general, it's a tough problem that language designers have seemed to try to avoid. They often just say "here's a compiler, it does compile-time things; everything after that is run-time, have fun". C is designed to be simple to implement, not the most flexible environment for computation. When you don't have the compiler available at runtime, or the ability to easily control when an expression is evaluated, you tend to end up with hacks in the language to fake common uses of macros, or users come up with Design Patterns to simulate having more powerful constructs. A simple-to-implement language can definitely be a worthwhile goal, but that doesn't mean it's the end-all-be-all of programming language design. (I don't use EVAL-WHEN much, but I can't imagine life without it.)
关于编译时和运行时的问题空间是巨大的,而且在很大程度上仍未被探索。这并不是说S.O.是进行讨论的正确场所,但我鼓励人们进一步探索这一领域,特别是那些对它应该是什么没有先入为主概念的人。这个问题既不简单也不愚蠢,我们至少可以给检察官指出正确的方向。
不幸的是,我不知道任何好的参考资料。CLTL2稍微讲了一下,但对于学习它并不是很好。