Search through blog..

Monday, December 5, 2011

What are Compiled and Interpreted languages

Compiled and Interpreted languages, When you start with programming, maybe these two words are one of the first things to learn.
Usually Programming languages are developed in order to make it easy for the Programmer to write source-code in a easily understandable and readable. And this source-code written by the Programmer has to be converted to Machine Language for the Computers to understand.
A programmer can directly write the source-code in Machine language too - however this will take several more time and unnecessary energy from the programmer, so thus the Programming languages.

Now, The process of converting the Programmer written source-code to Machine code can be done in two ways:
  1. Using a Compiler (Compiled languages) 
  2. Using an Interpreter (Interpreted languages)
So we need to get our source code converted into machine code somehow before it can run. Now luckily, this is not a big decision you have to worry about. Most languages you'll deal with will naturally fall into one or the other, but it is worth knowing the difference. You have your computer and I have my computer and you're going to write a program that you want me to run.
Now, with a compiled language, what happens is you write your source code and then you have a program called a compiler that will go through that source code and create a separate file that contains the machine code, and you just give me that file. This end result is sometimes referred to as an executable or an executable file because I can directly execute it. I can now just run your program. You keep your source code and I never see it.

Now, with an interpreted language on the other hand, you don't compile your source code beforehand. You just give me a copy of it. So I'll need my machine to interpret it whenever I want to run your program.

Interpreter is different to a compiler. It does this on-the-fly. We can think of it as going through your source code line by line and processing it on the spot. It does not save it as a separate machine code file. Now, you've used interpreted languages even if you don't know it. Whenever you've looked at a webpage with JavaScript, which if you've surfed the web for more than two minutes in your lifetime you have, this is what's been happening. The JavaScript has been sent to you over the web along with a bunch of other files like webpages and images and it's been sent as source code onto your machine, and your web browser has just interpreted that JavaScript so it can run that code.



Compiled languages:
Advantages of Compiled languages are, they are ready to run - no additional processing needed on the executable. They also run faster as there is a reduced step on the executables. And maybe the most important is that the Source code stays private.

Of course, like everything else, there are set of disadvantages too. First and most important is that the executables are not cross-platform - which means that the executables generated on a specific platform are usually working on the same type of platform where they are generated. Also this makes it inflexible. So in order to publish it to various platforms, we need to generate different executables specific to those particular platforms.

Interpreted languages:
Main advantage of an Interpreted language is that they run cross-platform. And are simpler to test and easier to debug on the end-user perspective as we have the source code visible.

And the disadvantages are that an additional interpreter is required to run the outcome and often are a bit slower due to this additional step. And also all the source code remains public.


Like it is shown in the above example, languages like C, C++ and other derivatives of C are Compiled languages and are often used where it is important to hide the source code. And languages like PHP, JavaScript which are mostly used for Web development are Interpreted languages.
Hope this helps. Happy programming.