Showing posts with label JIT. Show all posts
Showing posts with label JIT. Show all posts

Saturday, July 12, 2014

Java is Compiled and Interpreted Both

It seems little confusing for java beginner to understand the exact difference between compilation and interpretation. Java is compiled language because java code is compiled to get byte code. Java is interpreted language because this byte code is interpreted into native code.

Actually getting output from java program is two steps process compilation and interpretation. The executable used for these steps are java compiler (javac) and java interpreter (java). First step is compilation in which the java compiler compiles the source code to generate byte code in the form of .class file. Actually this byte code is not machine code but it is just an intermediate code. In the second step java interpreter uses this byte code to generate native code for under lying operating system. The JIT (Just In Time) compiler comes into action in second interpretation step. The JIT reads the byte code to generate native code dynamically so that program runs fast.


Finally in short, java is compiled to byte code which is then used by JVM where it gets interpreted to native code. So java is compiled and interpreted both.

What is JVM JRE and JDK

JVM, JDK and JRE are most commonly used in java programming. Many people think that these are same or gets confused about their differences. JVM, JDK and JRE are interlinked with each other and works together in cooperative manner.
  •  JVM (Java Virtual Machine): Interpreter + JIT
JVM is code executing component of Java that executes byte code (.class file). The output of Java compilation is byte code and only JVM can understand the byte code. JVM interprets this byte code to native code depending upon underlying processor and architecture. JVM has two main components interpreter and Just In Time (JIT) compiler. JIT is compiler which helps for optimizing byte code to machine code conversion and hence results into reduced execution time.  Note that Java is platform independent but JVM is not platform independent. Each JVM has its own structure of executable which differs from platform to platform. For each OS there is different JVM available. So JVM is highly platform dependent.
  • JRE (Java Runtime Environment): JVM + byte code verifier + class loader
JRE contains an environment for execution of Java program. JRE consist of JVM, class loader and byte code verifier. Class loader module loads all the .class files (byte code) and required libraries which are then verified by byte code verifier. Then this verified code is interpreted by JVM which creates native code.
  •  JDK(Java Development Kit): JRE + Development tool
JDK contains tools like compiler and debugger to develop Java program. It provides predefined set of libraries, API classes and other files which is used by JVM at runtime. It also includes JRE, interpreter (java), compiler (javac), document generator (javadoc).