A compiler converts higher programming language to machine level code.In case of java compiler converts the high level English like language to byte code -a flavor of machine level code.
An Interpreter converts one language to another , in the same level like, code interpreter from C++ to Java. In Java JIT(Just In time) code generator converts low level byte code to native machine code. Both they are at the same level.
Example of high level language to machine level conversion
Let us take an example of a program written in hypothetical language which prints the square of 20 on the screen:
x = 20
y = x*x
If this program is given to a compiler for this hypothetical language, then the compiler would first convert the program into assembly such as
Bird’s eye view how they work
This is why java is called both compiled and interpreted language at the same time. It has a important consequence on the speed. This is why java is also as well.
Dynamic linking –It links each time we execute java code.
Run time Interpreter: The conversion of byte code into machine code is done at run time. These are the two main reason why java is slower than other high level languages.
However Java has tried to over come the problem of architecture and environment dependency by introducing JVM(Java Virtual Machine).JVM specifies a detailed dummy CPU and instruction set that look exactly like standard assembly code. The java compiler transform the source program into the instruction set of JVM.The compiled Java source code is known as byte code instruction.Byte code is nothing but another assembly code.Next, the java interpreter which is specific to each environment(process, OS, bit) converts the byte code into native processor instruction before executing it.
Execution of Java:
java source(.java file)–>compiler–>.class file–> JVM–> Native machine code based on the execution environment–>result
Thus while converting a source code into Object code,compiling is carried out half way and interpreter does the remaining half. Java byte codes are architecture and Operating system independent.They are highly portable and can be executed on any system without any change.
Execution environment can be any combination:
- Intel+Linux etc
A byte code verifier in java interprets the execution of incoming byte codes and verifies whether the compiled code is strictly language complaint. In case it finds any illegal code, the JRE (Java runtime environment) rejects the code and refuse to execute.
|SQL||database query optimizer||Query Evaluation Plan|
|Java||javac compiler||Java byte code|
|English text||Natural Language Understanding||semantics (meaning)|
|Regular Expressions||JLex scanner generator||a scanner in Java|
|BNF of a language||CUP parser generator||a parser in Java|
|Tree-based interpreter||Perl ≤5, Ruby ≤1.8|
|Native code compilation||C, C++, Visual Basic 5 and 6|
|Bytecodes + dynamic compilation||Java ≥1.3, C#, Self, Smalltalk, Visual Basic .NET|