Why Java is both compiled and interpreted language?


In case of C/C++, the source code is directly converted to binary code by the compiler that can be executed by the OS to get output. This binary code generated on one OS does not execute on other OS. That is, the binary code generated on UNIX machine must be executed on UNIX only and similarly generated on Windows must be executed on Windows only. For this reason, C/C++ are known as platform-dependent languages. Now let us see the scenario on Java with respect to Why Java both compiled and interpreted.

But coming to Java, the scenario is completely a new one and different. Java is platform-independent language. To achieve the platform independency, the designers put two phases between source code to output – compilation stage and interpretation stage introducing a new concept called bytecode.

1. Compilation stage

Here compiler do the job. The source code is converted to bytecode by the compiler. Bytecode is a new concept introduced by Java designers not existing in C/C++. The bytecode looks very different consisting of some square boxes, unknown and unreadable characters and some scattered English characters. Following figure illustrates.

compiled and interpreted

Converting Java source code to bytecode is in known as compilation. The bytecode is not in a readable format by the Microprocessor (as Microprocessor knows only one language – binary language).

1. Interpretation stage (Execution stage)

Here, Java interpreter comes into picture. Java interpreter converts the bytecode into processor readable binary code. The binary code is executed by the Microprocessor and given output. Onething is to be noticed here. Different OS will have different architectures. The Java interpreter converts the bytecode in the binary code understood by that OS. That is, the same bytecode, if executed on Windows gives binary code understood by Windows OS and if executed on UNIX gets binary code understood by the UNIX architecture. That is, interpreters are very different. The Java interpreter working on Windows is different from UNIX interpreter. You must load a OS compatible Java interpreter. Interpreter is a part of JDK. So, JDK’s are different for different OS. The interpreter is known as JVM (Java Virtual Machine).

Now, I think you are sure of why Java is called both compiled and interpreted language.

More points to understand on compiled and interpreted stages

1. Java code is compiled to bytecode. The bytecode is an intermediate code between java and the machine code. So you need an interpreter (the JVM) to execute the byte code.

2. Its simple, the bytecode is portable between OS. That is, the bytecode generated on OS can be executed on a different OS (provided you load, the compatible JDK which works on that OS).

3. At Interpretation stage, the performance increases due to JIT compiler.

4. It is very efficient to compile a high-level language into some intermediate bytecode and then interpreting it into low-level binary code.

5. Other styles of getting output from the source code is to compile a high-level language into native code (understood by the underlying OS) like C++, Pascal, FORTRAN and to interpret the high-level code directly to get output like Python, PHP, Ruby where no bytecode or binary code file is generated.

2 thoughts on “Why Java is both compiled and interpreted language?”

  1. In more points to understand:
    At 3) You said Interpretation performance doesn’t increase with introduction of JIT compiler. But the sole purpose of JIT compiler is to increase performance..

    Moreover, Interpreter is not JVM , it is a part of JVM.

    AT 4) You said it is efficient to covert to intermediate byte code and then interpret. I don’t think so, for this reason Java suffered from performance reasons as compared to C/ C++ ..That why they introduced JIT for optimization.

    Java is both compiled and intrepeted because modern JVM’s use both Interpreter and JIT at same time…

Leave a Comment

Your email address will not be published.