似乎所有能够通过字节码实现的功能都可以在本地代码中更加轻松和快速地完成。理论上,您甚至可以通过在安装时将程序和库分发为字节码,然后编译为本地代码而不是JIT编译来保留平台和语言独立性。
那么一般情况下,什么情况下需要执行字节码而不是本地代码呢?
Dynamic code loading: One of the most interesting features of Java is that it allows code to be loaded into a running program. You can have a program that starts with a small amount of functionality, and then downloads additional code as needed (for example, from a web site) to add new features or extend its capabilities. This is much more flexible than having to download a whole new version of the program every time you want to change something. The only downside to this capability is that it makes it more difficult to control what code is running on your system. (There are security mechanisms in place that try to prevent rogue code from being downloaded and run, but they are not foolproof.)
字节码创建了一个额外的间接层。
这个额外的间接层的优点包括:
一些缺点:
与本地代码相比,字节码的可移植性和平台无关性可能是最显著的优点。