High-level programming languages such as BASIC, C, C++, and Java bridge the gap between the languages that humans understand and the language that a computer understands. These programming languages make use of words that we use, but they have a much stricter syntax to make it possible to translate them into the machine language (1s and 0s) required by the computer. It would certainly be nice if we could issue the computer an instruction like: "Print the numbers 1 through 10."
We are not quite there yet, but we are pretty close. Here's a program written in BASIC programming language that would accomplish this task:
FOR number = 1 TO 10
PRINT number
NEXT number
There are two ways to translate a program written in a high-level programming language into machine language. One approach involves using a program called a compiler and the other involves using a program called an interpreter.
Both a compiler and an interpreter translate source code into machine code. In other words, they both translate the program's instructions from the language of the programmer into the language of the computer.
Difference between bytecode and Machine Code
The key difference between 1) bytecode and 2) machine code lies in their levels of abstraction and how they are executed by a computer:
Bytecode:
Definition: Bytecode is an intermediate code that is generated by a compiler after the source code is compiled. It is not directly executed by the hardware; instead, it is run by a virtual machine (e.g., the Java Virtual Machine, JVM, or the Python interpreter).
Platform Independence: Bytecode is platform-independent. This means that the same bytecode can run on different platforms (e.g., Windows, Linux, macOS) as long as there is a virtual machine installed to interpret it.
Compilation Process: A language like Java compiles its source code into bytecode first. This bytecode is then interpreted or further compiled into machine code at runtime by the virtual machine (JVM in Java's case).
Performance: Bytecode requires an additional step to be translated into machine code, which can make execution slower compared to directly running machine code.
Machine Code:
Definition: Machine code is the lowest-level code that the computer's CPU understands directly. It consists of binary instructions that are executed by the hardware without the need for any interpretation.
Platform Dependence: Machine code is platform-dependent. Each CPU architecture (e.g., x86, ARM) has its own specific machine code. Code compiled for one type of processor will not run on another unless recompiled.
Compilation Process: Languages like C or C++ often compile directly into machine code specific to the target hardware. Once compiled, the code can be executed directly by the processor.
Performance: Since machine code is directly executed by the CPU, programs compiled into machine code generally run faster than those that are interpreted or just-in-time (JIT) compiled from bytecode.
Summary:
Bytecode is an intermediate, platform-independent code that runs on a virtual machine, offering portability but requiring interpretation or further compilation at runtime.
Machine code: is platform-specific binary code that runs directly on the CPU, offering high performance but lacking portability across different architectures.
A compiler translates an entire program from source code into machine code, and then this machine code is usually written to a file. Then, in a second step, the computer executes the machine code. The primary advantage of this approach is that because the program is already in machine code it runs very quickly. An important point to note is that a program compiled on a computer with one type of architecture generally can not be run on a computer with a different architecture. For example, the machine code produced by compiling a C program on a PC could not be run on a Mac. The program would need to be compiled using a different C compiler on the Mac.
An interpreter combines the translation and execution of a program. It translates source code line by line into machine code and executes each line as it is translated.
Because the source code has to be translated into machine code each time the program is executed, it generally takes longer to run interpreted programs than compiled programs. This concludes your introduction to computer basics. The next lesson wraps up this module.