15
votes

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?

7
There is no such thing as 'interpreted' in your definitions anyway :) When a JIT compiler compiles to machine language code it converts a bunch of instructions and performs optimizations on them. An interpreter just does that one thing at a time (so it is often much slower). Eventually, everything has to run on the machine anyway. (This is of course an oversimplification, but you get the point)Benjamin Gruenbaum

7 Answers

19
votes

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.

9
votes

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)?
3
votes

For the sake of precision, let's make clear this is not a Java programming language question, but a JVM feature.

In JVM first implementations, JIT didn't exist and bytecode was always interpreted. This was due to a design decision to make compiled code independent of the physical machine and OS running java, and is still valid today.

As a later refination, JIT was introduced in the JVM implementation for a faster execution, but the bytecode must still be valid and pass all the validations before being translated to binary. This way you keep the platform independence, all the sanity and security checks and you gain performance.

2
votes

Java is Hybrid Language i.e. it is both Compiled(work done upfront) and Interpreted(work done receiving-end).

Byte code is an IL(Intermediate Language) to Java. Java source code compiles to Bytecode by javac. Sometimes this byte code again compiles into Machine language which is referred as JIT(Just-In-Time) compilation.

JIT compilation is a way of executing computer code that involves compilation during execution of a program – at run time – rather than prior to execution. source

JVM(without JIT) interprets the java Intermediate Language byte code to native machine language as follows:

enter image description here

Source

JVM is an abstract computing machine, it has several implementations:

  • HotSpot (Interpreter + JIT compiler) : the primary reference Java VM implementation. Used by both Oracle Java and OpenJDK.

  • JamVM (Interpreter) Developed to be an extremely small virtual machine compared to others. Designed to use GNU Classpath. Supports several architectures. GPL.

  • ART (Interpreter + AOT compiler i.e. Ahead-of-time compilation) Android RunTime is an application runtime environment used by the Android operating system replacing Dalvik (interpreter + JIT compiler).

List of Java virtual machines

1
votes

javac is a compiler and it converts java code into bytecode (see bytecode) which is easy to run on any machine if we have a JVM (java Virtual Machine). and interpreter converts java bytecode into machine code.

0
votes

It serves two purposes. The first is to ensure that the code is syntactically and semantically correct. Secondly, the compilation process produces byte-code. As you note, this is an architecture-agnostic intermediate language that can be interpreted or just-in-time compiled to native code by the JVM for a specific machine architecture. By compiling to byte-code, much of the overhead associated with compilation can be done in advance, leaving the JVM to generate native code from or interpret byte-code that has been thoroughly and rigorously checked beforehand.

0
votes

Unlike other programming language java is compiled and interpreted language. Java IDE acts as a compiler and JVM(java virtual machine) behave like an interpreter. i.e. when any program let say Hello, is saved after compiling as Hello.java and after compiling this file we get Hello.Class extension file is called as class-file, byte-code or intermediate code. Byte-code is not dependent for any specific machine so it is also called as intermediate code. To convert this byte-code into machine code or machine understandable format JVM is used which is different for different operating system. JIT(Just in Time Compiler) is a part of JVM that is enabled by default compiles the bytecode into the native machine code compiling in 'just in time'.