Imagine a guy who just started programming. He searched the google for the articles on his “favorite” programming — the choice he made subconsciously, because his friends told him so. He found people talking about the programming, but he misunderstood things, including “compiled”, “interpreted”, etc., because his private language (the language he speaks) has different meanings of the words written there. To reduce the complexity in another newbie programmer, I am writing a short tutorial to highlight the difference between compiled and interpreted languages.
Compiled vs Interpreted Languages
Compiled languages are those in which the code is compiled line-by-line, which means that a compiler tries to understand every line of code in a program (including different files within a single project), and then translates it into the machine language of the architecture, which often happens after it’s translated to assembly instructions in .obj files – also known as object files, which are then linked together to create an executable file which a user can run on a target machine.
Interpreted languages, on the hand, are the ones that translates the code to the bytecode (i.e., the code format that is understood by the virtual machines) line-by-line on the fly, which means that it will start interpreting the code from the start and then execute the results through virtual machines, which could be seen through the console (in the form of logs or actual results). Virtual machines are provided by the vendors of the language to convert the bytecode to the machine language of the target machine.
Many programming languages are both interpreted and compiled, including Java, C#, Python, etc. These are called “bytecompiled” languages. In these languages, the code is compiled by the compiler to the bytecode of the language which can be run on the fly through the Virtual Machines (VMs) that are specifically developed for translating the code to platform dependent machine code on the fly. So, for instance, if you have different .class files which are basically Java bytecode that are independent of the architecture, then the JVM (which should be installed on the target machine) can convert the .class files in a real-time to the machine language of the computer architecture on which the program is running (i.e., x86, x64).
Which ones are faster?
As compiled languages need to only compile for once, the compiler make sure to add every kinds of optimization for the particular machine on which the program is compiling. This results in the faster execution of program on the average with the cost of slow compile-build-run process, as you would need to compile the whole program when you change a single-line of code, though modern compilers are getting better in recognizing the areas that need to be recompiled through carefully investigating the object files (.obj).
Interpreted languages, on the hand, require a two-step process: first the compiler compiles the whole program into the bytecode. Now, a compiler doesn’t know anything about the machine on which the program will run, so the bytecode which is produced by the compiler is platform neutral or independent. Once the bytecode is available, it is then run by the virtual machines installed on the target machine. At this stage, a virtual machine can optimize the bytecode as per the machine specifications, but the optimization isn’t very good as compared to the compiler in the compiled languages.
Which ones are better?
I don’t think there is a conclusive answer to the question. These days interpreted languages are quite in-demand due to the fact that virtual machines have been greatly optimized since the past few years (due to the addition of JIT
techniques) plus the average users are using the modern computers that can run the code through virtual machine in almost the same speed as if it is written in a compiled language.
Most interpreted languages can be used interactively where you can run the code on the fly through the shell line-by-line (REPL
). For instance, Python provides an interactive shell through which you can type the function or statement to do something (such as, printing the number, adding the numbers, etc.), and it will work instantly and give the results to the console, even on the website
. There are few compiled languages that can run interactively, such as, Clojure (Lisp), Haskell, etc. Interpreted languages can also be very convenient for dynamic typed languages
Please note that compilation or interpretation is a matter of implementation, not the language itself. Most often than not, many languages considered “interpreted” also provides a compiler to translate the code to machine language. But, as newbie programmers tend to search “compiled vs interpreted languages”, we were forced to use such title for our tutorial.
If you have any question, then you can ask in the comment section below!