Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java compiler/interpreter

Tags:

Why we do we say that Java is a compiled and interpreted language?

What is the advantage of this (being compiled and interpreted)?

like image 440
JavaUser Avatar asked Jul 05 '10 14:07

JavaUser


People also ask

What is Java compiler and interpreter?

As the Java compiler compiles the source code into the Java bytecode. In the same way, the Java interpreter converts or translates the bytecode into the machine-understandable format i.e. machine code, after that the machine code interacts with the operating system.

Is JVM is a compiler or interpreter?

JVM have both compiler and interpreter. Because the compiler compiles the code and generates bytecode. After that the interpreter converts bytecode to machine understandable code. Example: Write and compile a program and it runs on Windows.

What is Java interpreter?

Interpreter in Java is a computer program that converts high-level program statement into Assembly Level Language. It is designed to read the input source program and then translate the source program instruction by instruction.

How does Java use 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.


2 Answers

Java is compiled to an intermediate "byte code" at compilation time. This is in contrast to a language like C that is compiled to machine language at compilation time. The Java byte code cannot be directly executed on hardware the way that compiled C code can. Instead the byte code must be interpreted by the JVM (Java Virtual Machine) at runtime in order to be executed. The primary drawback of a language like C is that when it is compiled, that binary file will only work on one particular architecture (e.g. x86).

Interpreted languages like PHP are effectively system independent and rely on a system and architecture specific interpreter. This leads to much greater portability (the same PHP scripts work on Windows machines and Linux machines, etc.). However, this interpretation leads to a significant performance decrease. High-level languages like PHP require more time to interpret than machine-specific instructions that can be executed by the hardware.

Java seeks to find a compromise between a purely compiled language (with no portability) and a purely interpreted language (that is significantly slower). It accomplishes this by compiling the code into a form that is closer to machine language (actually, Java byte code is a machine language, simply for the Java Virtual Machine), but can still be easily transported between architectures. Because Java still requires a software layer for execution (the JVM) it is an interpreted language. However, the interpreter (the JVM) operates on an intermediate form known as byte code rather than on the raw source files. This byte code is generated at compile time by the Java compiler. Therefore, Java is also a compiled language. By operating this way, Java gets some of the benefits of compiled languages, while also getting some of the benefits of interpreted languages. However, it also inherits some limitations from both of these languages.

As Bozho points out, there are some strategies for increasing the performance of Java code (and other byte code languages like .Net) through the use of Just in Time (JIT) compilation. The actual process varies from implementation to implementation based on the requirements, but the end-result is that the original code is compiled into byte code at compile time, but then it is run through a compiler at runtime before it is executed. By doing this, the code can be executed at near-native speeds. Some platforms (I believe .Net does this) save the result of the JIT compilation, replacing the byte code. By doing this, all future executions of the program will execute as though the program was natively compiled from the beginning.

like image 88
Chris Thompson Avatar answered Sep 21 '22 07:09

Chris Thompson


Why do we say Java is compiled and interpreted language.

Because source code (.java files) is compiled into bytecode (.class files) that is then interpreted by a Java Virtual Machine (also known as a JVM) for execution (the JVM can do further optimization but this is anoher story).

What is the advantage over this(being compiled/interpreted)

Portability. The same bytecode can be executed on any platform as long as a JVM is installed ("compile once, run anywhere").

like image 33
Pascal Thivent Avatar answered Sep 22 '22 07:09

Pascal Thivent