Why Java is Both Interpreted and Compiled Language?

0
21
Spread the love
  • 1
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
    1
    Share

In my last post , I have explained how java is a platform independent language.In this post we will see how java is both Interpreted and compiled language.
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.
alt text
Source: http://stackoverflow.com/questions/3618074/what-is-the-difference-between-compiler-and-interpreter
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:

High level language
x = 20
y = x*x
print y



If this program is given to a compiler for this hypothetical language, then the compiler would first convert the program into assembly such as

mov ax,20
mov bx,ax
mul bx,bx
push bx
call _print_


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+windows 
  • AMD+Linux
  • 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.


Source Language
Translator Target Language
LaTeX Text Formater PostScript
SQL database query optimizer Query Evaluation Plan
Java javac compiler Java byte code
Java cross-compiler C++ 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

Source: http://lambda.uta.edu/cse5317/notes/node3.html

Execution strategy Languages
Bytecode interpreter UCSD and Berkeley Pascal, JavaScript, Java ≤1.2, Python, Ruby 1.9, Smalltalk-80, Visual Basic ≤4
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

Source: http://www.antlr.org/wiki/display/ANTLR3/The+difference+between+compilers+and+interpreters

Don't miss out!
Subscribe To Newsletter

Receive top technical news, lesson ideas, travel tips and more!

Invalid email address
Give it a try. You can unsubscribe at any time.

Spread the love
  • 1
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
    1
    Share

LEAVE A REPLY

Please enter your comment!
Please enter your name here