Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why Java is both compiled and interpreted language when the JIT also compiles the bytecode?

I read that, a java source code is compiled into 'bytecode' then it is 'Compiled' again by JIT into 'machine code'. That is, the source code is first compiled into a platform independent bytecode and then compiled again to a machine specific code. Then why it is called as both interpreted and compiled language? Where the interpretation takes place?

like image 667
Ragul Avatar asked Jan 02 '14 10:01

Ragul


People also ask

Why is Java considered both interpreted and compiled language?

Java can be considered both a compiled and an interpreted language because its source code is first compiled into a binary byte-code. This byte-code runs on the Java Virtual Machine (JVM), which is usually a software-based interpreter.

Why do we need both compiler and interpreter?

Both compilers and interpreters are used to convert a program written in a high-level language into machine code understood by computers.

Does Java need both compiler and interpreter?

Java is first machine independent programming language; 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.

What makes compiled interpreted or JIT compiled differ from each other?

Static compilation converts the code into a language for a specific platform. An interpreter directly executes the source code. JIT compilation attempts to use the benefits of both. While the interpreted program is being run, the JIT compiler determines the most frequently used code and compiles it to machine code.


2 Answers

There is a bit of misunderstanding here.

In normal circumstances java compiler(javac) compiles java code to bytecodes and java interpreter(java) interpretes these bytecodes(line by line), convert it into machine language and execute.

JIT(Just in time) compiler is a bit different concept. JVM maintains a count of times a function is executed. If it exceeds the limit then JIT comes into picture. java code is directly compiled into machine language and there on this is used to execute that function.

like image 55
Aniket Thakur Avatar answered Oct 27 '22 10:10

Aniket Thakur


Java is a programming language.

It has a specification (the JLS) that defines how Java programs should act.

As a language itself, it does not specify how it should be executed on different platforms. The way it runs, with a JIT or without a JIT is entirely implementation based.

  • If I write a Java runtime tomorrow that does not do JIT compilation at all I can call Java interpreted.

  • If I take a Java machine (and people seriously made those) that uses Java bytecode as assembly, I can call Java strictly compiled.

A lot of other languages do this:

  • Is python an interpreted language? (CPython) or is it JITed (PyPy)?
  • Is Lua interpreted (old lua interpreters) or is it compiled (LuaJIT)?
  • Is JavaScript interpreted (IE6 style) or is it compiled (v8)?
like image 37
Benjamin Gruenbaum Avatar answered Oct 27 '22 09:10

Benjamin Gruenbaum