Byte code acts as a bridge between high-level source code and machine code. This intermediate code is produced when source code, written in a high-level programming language, is compiled. Virtual machines, such as the Java Virtual Machine (JVM), process byte code.
Byte code itself isn't executable. It must be interpreted into machine code, which the computer can understand. Programs compiled into byte code can run on any system with a compatible JVM, making Java platform-independent. This feature makes byte code portable.
Machine Code
Machine code consists of instructions directly understood by the computer's CPU. Represented in binary (0s and 1s), machine code is the lowest-level form of code and is distinct from both byte code and source code. It is generated through compilation or interpretation and is also known as machine language.
The following illustration demonstrates the process of converting Java source code into byte code and then into machine code:
Related Posts
Difference between Byte Code and Machine Code
S.NO | Byte Code | Machine Code |
---|---|---|
1 | Byte code includes binary, hexadecimal, and macro instructions like (new, add, swap, etc.). It is not directly understandable by the CPU and is designed for efficient execution by software such as a virtual machine. | Machine code consists of binary instructions directly understood by the CPU. |
2 | Byte code is considered intermediate-level code. | Machine code is considered low-level code. |
3 | Byte code is non-runnable and generated after the compilation of source code. It relies on an interpreter to be executed. | Machine code is a set of instructions in binary format directly executed by the CPU. |
4 | Byte code is executed first by the virtual machine and then by the CPU. | Machine code is not executed by a virtual machine; it is directly executed by the CPU. |
5 | Byte code is less specific to a particular machine than machine code. | Machine code is more specific to the machine than byte code. |
6 | Byte code is platform-independent because it depends on the virtual machine. Any system with a virtual machine can execute it, regardless of the platform. | Machine code is not platform-independent. The object code of one platform cannot run on a different operating system, as it varies based on system architecture and native instructions. |
7 | Not all source code needs to be converted into byte code for execution by the CPU. Some source code written in a high-level language is first converted into byte code, then into object code for CPU execution. | All source code must be converted into machine code before it can be executed by the CPU. |