字节码与机器码的区别?
因为从毕业就一直从事 iOS 开发的开发,大学也是学的 C++,Android 也没好好学,导致我对字节码就很陌生,但是随着自己的知识面越来越广,字节码就映入眼帘,但是我却不知道它是什么,和它的作用又是什么?它和我们平时生成的机器码有什么区别呢?
在随笔里面我就很详细的说了,因为本身也是现学现卖,只是记录作用,为了方便自己以后查看。
在海外知乎 Quroa 上找到一位 Google 工程师如下回复: https://www.quora.com/What-is-the-difference-between-byte-code-and-machine-code-and-what-are-its-advantages
仅供参考!
Byte code is similar to machine code (but not machine code) which can be understood by a run-time system. It is sort of a programming language (as machine code also is) which is interpreted by a run-time system so that the code can be executed on a system. The run-time system ultimately turn those Byte code into actual machine code while it executes them on the computer.
For example, Java (programming language) compiles its program to Byte code. Then Java Runtime System (JRE) interprets those Byte code into machine code when it runs on Java Virtual Machine (JVM), the underlying system. JVM is the underlying system which interacts with the operating system on the computer whenever a piece of Java Byte code is being run. JVM manages resources (e.g - garbage collection) on behalf of the program being executed.
The advantage of Byte code is that it only depends on the run-time system and the virtual machine it is being run on. Therefore, it is platform-independent. Any machine can run any operating system. If the virtual machine (e.g - JVM) is installed on that operating system, then the Byte code can be run. Therefore, it is really well-suited for portability. A program can be compiled into Byte code and sent over to any machine to be run which has a run-time installed. The program does not need a recompilation. This is, in contrast with languages like C or C++. These languages are directly compiled to machine code and hence, are platform-dependent.
ByteCode 类似于运行时系统可以理解的机器代码(但不是机器代码)。它是一种编程语言(就像机器代码一样),它由运行时系统解释,以便 code 可以在系统上执行。运行时系统最终将这些 ByteCode 转换为实际的机器码,同时在计算机上执行它们。
例如,Java(编程语言) 将其程序编译成字节码。然后,Java Runtime System(JRE) 在底层系统 Java 虚拟机(JVM) 上运行时,将这些字节代码解释为机器码。JVM 是每当运行一段 Java 字节代码时与计算机上的操作系统交互的底层系统。JVM 代表正在执行的程序管理资源(例如,垃圾收集)。 ByteCode 的优势在于它只依赖于运行时系统和它正在运行的虚拟机。因此,它是独立于平台的。任何机器都可以运行任何操作系统。
如果虚拟机(例如,JVM) 安装在该操作系统上,则可以运行 ByteCode。因此,它非常适合于可移植性。程序可以编译成字节码,然后发送到任何安装了运行时的机器上运行。该程序不需要重新编译。这与 C 或 C++等语言形成对比。这些语言被直接编译成机器码,因此是平台相关的。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
上一篇: Git 简明教程 - 基础篇
下一篇: 彻底找到 Tomcat 启动速度慢的元凶
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论