Engineering Full Stack Apps with Java and JavaScript
A compiler is a computer program that transforms source code written in a programming language into another computer language, often in a binary form, that is understood by the computer platform.
A compiler usually transforms a high-level programming language to machine code specific to a platform, so that it can then be directly executed in that platform.
An interpreter is a computer program that directly executes instructions written in a programming or scripting language, without previously compiling them into a machine language program.
Compiled languages uses a compiler to translate the source code to machine code specific to a platform, and then this compiled form is taken to any computer with that platform, and executed; compile once and execute as many times.
Interpreted languages compiled line-by-line as as that line was about to be executed.
Performance was not the best with complete interpreted languages, as, if a loop or subroutine caused certain lines to be executed multiple times, they would be recompiled every time.
Advantages of compiled approach
We can have compile time type checking, that prevent type mismatch or syntax issues coming up during run time.
We can have code optimizations during the compile time looking into whole code and run the optimized code during run time.
The code will run faster as it is already compiled for a particular platform and can be directly executed without needing to have to do the translation every time.
Disadvantage for the compiled approach:
Not Portable: The executable code generated after compilation is platform dependent and there should be a different executable for different platform.
For such software, there need to be a different executable for every different platform (Windows, Linux, Mac etc).
Advantages of interpreted approach
Code is more portable, as the same program can be shipped to any platform. An interpreter specific to a platform may convert this code (or executable) line by line to that platform specific machine language and then execute.
Disadvantages of interpreted approach
There is no compile time type checking and actual issues might come up during run time.
Compile time code optimizations are not possible. This is because, in interpreted approach, only one line of code is available at a time to the interpreter.
The code will run slower as it is converted to machine language while executing.
Examples
C++ and Fortran are examples of compiler based languages.
PHP and perl are examples of interpreter based languages.
Newer programming languages like Java, Python etc. uses a combination of compiler and interpreter; a compiler may output some form of intermediate bytecode (Java's .class files or Python's .pyc files), which is then executed by a bytecode interpreter (Java Virtual Machine or Python virtual machine).