字节码与机器码的区别?
因为从毕业就一直从事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++等语言形成对比。这些语言被直接编译成机器码,因此是平台相关的。
最后更新于