Shahzaib Mazari
Shahzaib Mazari

Reputation: 444

How compiler and interpreter both are used in one language?

I have read enough explanation about the definition of compiler, interpreter and "things" that use both. I didn't find how compiler and interpreter both are used in one language.

Upvotes: 5

Views: 7147

Answers (2)

sabeen kanwal
sabeen kanwal

Reputation: 607


Java is one of the first machine independent programming languages; it uses both compiler and interpreter. Java compilers are designed in such a way that converts source code into platform independent form i-e byte codes. These byte codes are then converted to machine code by interpreter. This is how compiler and interpreter both used in one language. Any system having JVM will run these byte codes.

Java program  byte code  interpreted by VM  machine language

summary :

java compiler convert source code in to an intermediate language known as bytecode. This bytecode only can be executed in a virtual environment called JVM. Java virtual machine. JVM is an interpreter to java bytecode. It converts bytecode into machine language and executes line by line.

this is how both compiler and interpreter used in one language..if it found useful you may mention

Upvotes: 4

marzelin
marzelin

Reputation: 11610

In Java the source code is first compiled to bytecode and then it's run by an interpreter (JVM - Java Virtual Machine).

bytecode is machine code for a virtual machine.

In Javascript there's a runtime (engine) that does just in time compilation (JIT). Basically, at execution time it's given a source code which it immediately converts to native code and then the code is executed. In Chrome's engine there are two modules that do compilation: one can execute code fast but the code isn't much optimized (ignition interpreter) and the other produces a highly performant code but compilation takes more time (turbofan compiler).

Why use both:

  • portability - when you use intermediate representation that is compiled AOT you can take this bytecode and run it on any architecture for which a Virtual Machine is provided. You can push the same Java bytecode to clients on Mac, PC or Linux. If they have JVM installed the code will run. For C or C++ you have to ship different program executable for each architecture
  • fast initial start and decent execution performance - compilation takes time (and the more optimized code the more time it needs for compilation generally) but nobody likes to wait. It's better to produce something that is not perfect (ignite phase) and then gradually improve the code by compiling hot paths into highly optimized machine code (turbofan phase). This is especially plausible today where we have CPUs with many cores but we are not able utilize them all because creating programs with many parallel threads is hard (so one core can execute program while the other can optimize code in the meantime)

Upvotes: 4

Related Questions