简体   繁体   English

Java 中的“运行时”是什么意思?

[英]What does "runtime" mean in Java?

a JVM implementation is an interpreter that converts a bytecode to machine code. JVM 实现是将字节码转换为机器码的解释器。 But in the meantime a JVM implementation throws runtime error.但与此同时,JVM 实现会引发运行时错误。 Does it mean that an interpreter checks runtime error in Java?这是否意味着解释器检查 Java 中的运行时错误? Or does it mean that Java's runtime occurs during the stage from bypecode to machine code?或者这是否意味着Java的运行时发生在从字节码到机器码的阶段?

It means that during run time of the application (when app was actually running) some exception was thrown eg attempt to divide by 0 which happen to be an user input.这意味着在应用程序的运行期间(当应用程序实际运行时)抛出了一些异常,例如尝试除以 0,这恰好是用户输入。

After research, I've found that a classical compiler converts source code to machine code, ready for computer to execute it, while a classical interpreter directly execute source code.经过研究,我发现经典编译器将源代码转换为机器码,供计算机执行,而经典解释器直接执行源代码。

In C, the compiler converts its source code to almost machine code (an object file).在 C 中,编译器将其源代码几乎转换为机器代码(object 文件)。 When we "run" a C program, it means the machine execute the object file.当我们“运行”一个 C 程序时,这意味着机器执行 object 文件。 So the runtime error is thrown by the machine.所以机器会抛出运行时错误。

While in Java, the compiler converts its source code to bytecode, an intermediate code between source code and machine code.而在 Java 中,编译器将其源代码转换为字节码,即源代码和机器代码之间的中间代码。 After compilation, an non-classical interpreter (an bytecode interpreter) directly executes this bytecode.编译后,一个非经典的解释器(字节码解释器)直接执行这个字节码。 So the runtime error is thrown by this kind of interpreter, which is called "virtual machine" in Java, as it acts like the machine in C which execute the program.所以运行时错误是由这种解释器抛出的,它在 Java 中称为“虚拟机”,因为它的行为类似于 C 中执行程序的机器。

Return to the question I asked, the interpreter (JVM) in Java throws runtime error because it acts like machine itself rather than acting as an intermediate stage before the actual execution.回到我问的问题,Java 中的解释器 (JVM) 会引发运行时错误,因为它就像机器本身一样,而不是在实际执行之前充当中间阶段。

Here is a resource which explains types of errors in Java, including the Runtime Error. 这是一个资源,它解释了 Java 中的错误类型,包括运行时错误。 @Antoniossss has a brief explanation if you like.如果您愿意,@Antoniossss 有一个简短的解释。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM