4.6.3.1 Types of program translator

Most programming languages can be edited wthin a text editor and/or a dedicated IDE. However, the text alone is not good enough to be ran. Computer programs must be processed either by a compiler or an interpreter in order to execute the code.

Compiler

A compiler is a computer program which converts code into a binary. This binary can be ran multiple times, without recalling the compiler. The resultant file from the compiler can only run on the same computer architecture that it was compiled for, and may be incompatible due to architecture, operating system incompatibilities, etc. Because a binary is generated, it may take time for the computer program to compile. After compilation, running the program will take less time.

The programmer's code sometimes requires extra libraries and object code. When this is required, a linker includes these libraries (like ::std) and combines them into the final binary by a linker.

Examples of compiled languages include:

  • Go
  • C
  • C++

Advantages

  • Binaries are faster compared to interpreted code

Disadvantages

  • Entire code has to be written before it can be tested
  • Errors require recompilation of the binary
  • Binary only runs on that architecture and that operating system

Interpreter

An interpreter is an intermediate program which reads code line by line, statement by statement and runs the code in real time. This allows programmers to edit the code in real time without waiting for recompliation. However, interpreted languages have to have the code distributed with the interpreter. This may mean the source code could be leaked and stolen by others. An interpreter can run code on all compatible clients.

Examples of interpreted languages include:

  • Python
  • JavaScript (Node.js)

Advantages

  • Faster to test
  • Can immediately fix errors (as interpreter runs up to the error)
  • Can run on multiple systems (with the interpreter installed on multiple systems)

Disadvantages

  • Translation takes longer to do
  • Uses more RAM
  • Must be interpreted each time the program is ran

Java?

Java uses a combination of both to merge some of the benefits of each. (While also combining the negatives)

Java uses a compiler to compile java code into java bytecode. This bytecode is read by the JVM (The Java Virtual Machine) which processes the bytecode.

Some implementations of the JVM uses just-in-time compilation, where the bytecode is converted into instructions the host computer can understand. Other implementations may instead use an interpreter to run the code.

Because of the complexity of how Java is processed and executed, there is argument{: .ext} about whether or not it uses one, both, none and/or parts of compilation and intepreting.

"no u"

Questions

Answer

  1. Do interpreters produce a binary?
  2. Which method is faster to run code and why?