java - 字节码与 native 代码相比有什么优势?

标签 java .net bytecode

您可以用字节码做的任何事情似乎都可以在 native 代码中轻松快捷地完成。理论上,您甚至可以通过以字节码分发程序和库,然后在安装时编译为 native 代码,而不是 JIT 来保持平台和语言的独立性。

那么一般来说,您希望什么时候执行字节码而不是原生代码?

最佳答案

SGI 的 Hank Shiffman 说(很久以前,但这是真的):

There are three advantages of Java using byte code instead of going to the native code of the system:

  1. Portability: Each kind of computer has its unique instruction set. While some processors include the instructions for their predecessors, it's generally true that a program that runs on one kind of computer won't run on any other. Add in the services provided by the operating system, which each system describes in its own unique way, and you have a compatibility problem. In general, you can't write and compile a program for one kind of system and run it on any other without a lot of work. Java gets around this limitation by inserting its virtual machine between the application and the real environment (computer + operating system). If an application is compiled to Java byte code and that byte code is interpreted the same way in every environment then you can write a single program which will work on all the different platforms where Java is supported. (That's the theory, anyway. In practice there are always small incompatibilities lying in wait for the programmer.)

  2. Security: One of Java's virtues is its integration into the Web. Load a web page that uses Java into your browser and the Java code is automatically downloaded and executed. But what if the code destroys files, whether through malice or sloppiness on the programmer's part? Java prevents downloaded applets from doing anything destructive by disallowing potentially dangerous operations. Before it allows the code to run it examines it for attempts to bypass security. It verifies that data is used consistently: code that manipulates a data item as an integer at one stage and then tries to use it as a pointer later will be caught and prevented from executing. (The Java language doesn't allow pointer arithmetic, so you can't write Java code to do what we just described. However, there is nothing to prevent someone from writing destructive byte code themselves using a hexadecimal editor or even building a Java byte code assembler.) It generally isn't possible to analyze a program's machine code before execution and determine whether it does anything bad. Tricks like writing self-modifying code mean that the evil operations may not even exist until later. But Java byte code was designed for this kind of validation: it doesn't have the instructions a malicious programmer would use to hide their assault.

  3. Size: In the microprocessor world RISC is generally preferable over CISC. It's better to have a small instruction set and use many fast instructions to do a job than to have many complex operations implemented as single instructions. RISC designs require fewer gates on the chip to implement their instructions, allowing for more room for pipelines and other techniques to make each instruction faster. In an interpreter, however, none of this matters. If you want to implement a single instruction for the switch statement with a variable length depending on the number of case clauses, there's no reason not to do so. In fact, a complex instruction set is an advantage for a web-based language: it means that the same program will be smaller (fewer instructions of greater complexity), which means less time to transfer across our speed-limited network.

因此,在考虑字节码与原生代码时,请考虑您希望在可移植性、安全性、大小和执行速度之间做出哪些权衡。如果速度是唯一重要的因素,那就选择原生。如果其他任何一个更重要,请使用字节码。

我还要补充一点,为每个版本维护一系列针对同一代码库的操作系统和架构目标编译可能会变得非常乏味。在多个平台上使用相同的 Java 字节码并让它“正常工作”是一个巨大的胜利。

关于java - 字节码与 native 代码相比有什么优势?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48144/

相关文章:

java - 为 SPA 前端配置 Spring Boot

c# - 如何显示来自非 GUI 相关类的 GUI 应用程序的错误消息?

.NET NUnit 测试 - Assembly.GetEntryAssembly() 为空

java - ASM 从堆栈帧中获取精确值

java - 异常 : Session Issue ids for this class must be manually assigned before calling save(): org. me.Testservices.TblUsers

java - 在 neo4j 中出现以下错误 "org.neo4j.kernel.api.exceptions.TransactionFailureException: Transaction rolled back even if marked as successful"

java - JBoss 7.1.1.Final - 从 WAR 文件内的 META-INF 加载资源失败

c# - UserPrincipal.Save() 导致 ObjectDisposedException

java - 从 Java 可见的 Scala 中的包私有(private)范围

java - Java字节码是由JVM顺序执行的吗?