Tuesday, 11 July 2017

Virtual machine, statically typed vs dynamically typed, WORA, machine code vs byte code, compiled vs interpreted languages

 

1. Compiled vs interpreted languages:

Compiled languages: The languages which when compiled is directly converted to target machine format. For example if we have
a = b + c
in our source program, it can be directly converted to machine level language like
ADD B,C
MOV B,A
Language like this need not be compiled again and again for execution. It can be compiled once and executed any number of times.
Some of the languages which fall into this category are C, C++ and C# etc.


Interpreted languages: The languages which are not directly converted into the destination machine language are interpreted languages. In these kinds of languages the source code is converted into an intermediate representation by the interpreter and then finally into the machine format.
If we take the example of adding b and c i.e “b+c”. This high level language code is first read by the interpreter which then converts it into it’s equivalent interpreted format let’s say add(b,c). This function would call the machine instruction ADD.
The source code of interpreted languages cannot be executed in one shot. Every line is interpreted and executed independently by the interpreter.
Every time the program needs to be executed it needs to be interpreted. So it’s slow compared to compiled languages.


Actually the languages are not interpreted or compiled it’s how we implement them. But if are to classify languages based on this category this is the explanation.

2. Machine Code and byte code:

Machine code: A machine code is the set of instructions that can be directly executed by the cpu. It is hardware dependent and every machine has its own machine code instruction set.
Machines which have the same architecture often have the same instruction set and thereby have the same interpretation for the same source program.
For example intel processor might have the follwing instruction for adding -> 00101010.
And an MOTOROLA processor will have different instruction for adding something like -> 10100010
A machine code of an intel machine can not be run on an motorola machine.


Byte code: A byte code is an intermediate representation of the java source code. Java is compiled and interpreted language. Once we compile a java program (.java file) it is converted to an intermediate representation called bytecode. The .class file is the byte code.


The reason we call it as bytecode is because every instruction in the .class file is of one byte. This .class file is given to java interpreter which is specific to a machine. This interpreter takes the .class file and execute it line by line. javac is the compiler and java is the interpreter.

3. Virtual machine:

A virtual machine is a computer file that works as a actual computer. It is same as creating a computer inside another computer. An operating system can run another operating system giving the user the feeling that it has its own machine.
Using virtual machine we can use the hardware of the machine same as the parent operating system.
For example: We can use windows as the base operating system and on top of it we can install VmWare which is used to create an instance of the hardware of the machine. It can then be used to run ubuntu. And ubuntu works as if it is the parent process.

4.WORA:

WORA stands for write once run anywhere. This is one of the key feature of java which makes it popular. Java source code is compiled and converted to an intermediate form which is called bytecode.
This byte code is then interpreted by an interpreter, which is specific to the machine. So to run a java program we only need the java interpreter for that platform. It is because of this feature it is WORA.

5. Statically typed and dynamically typed languages:

Statically typed:
All the languages in which the type of the variable is known during the compiled time. It is the reponsibility of the programmer to specify to which type the variable belongs. If it is not done the compiler raises error.
Some of the statically typed languages are C, C++, Java etc.
Dynamically typed: All the languages in which the type is associated with the run time values not the variables are dynamically typed. So a variable can be assigned any value in a program. For example:
Employee_name = “Himanshu”
Employee_name = 48
There will be no compilation error in the above scenario in case of dynamically typed languages.
But it is not allowed in statically typed languages.

This was all about this topic. As always if you like the post, share it. If you have any queries, or you want to suggest something you can always ping me on Facebook or mention in the comments section below. I will be happy to help. You can follow me on Facebook or google+. Don't forget to follow the blog post.