0.6 – Java as a platform independent language

by subbu on November 5, 2014

How java works like a platform independent language?

Reason behind the popularity and demand for java is its platform independency. Now we will see how java works like a platform independent language.

The source code (program) written in java is saved as a file with .java extension. The java compiler “javac” compiles the source code and produces the platform independent intermediate code called byte code. It is a highly optimized set of instructions designed to be executed by the JVM (will be covered later).

The byte code generated by the compiler is not native to any platform because java compiler doesn’t interact with the local platform while generating byte code. Say for example, byte code generated on Windows is same as the byte code generated on Linux for the same java code.

The Byte code generated by the compiler would be saved as a file with .class extension. As it is not generated for any platform, can’t be directly executed on any CPU.

Java compiler

There must be a separate mechanism to execute the byte code generated by the java compiler. Now we will see different tools that help in executing the byte code.

When we load and start a java program

  • The class loader loads the .class file (byte code) and stores into the memory which is in application scope.
  • The byte code verifier verifies the byte code, allows further if it feels no threat to the computer. It helps to enforce the security from unauthorized access to the local file system and hardware by non-byte code.
  • The Java Virtual Machine (JVM) translates the byte code into native executable code according to the local platform and which would be executed by the CPU.

Java virtual machine

So, we need class loader, byte code verifier and JVM to execute the byte code on any computer. Technically class loader, byte code verifier and JVM together called as JRE (Java Runtime Environment). So, every computer must be preloaded with JRE to run any java application.

Naturally java is not a platform independent language but works like a platform independent language with preloaded JRE on every computer. It justifies Write Once, Run Any where (WORA) approach.

Write Once Run Any where

Here the byte code generated by the java compiler is loaded on to different platforms like Windows, Linux and Mac. The Java Virtual Machine’s belongs to these platforms interprets and produces executables according to their local platforms and that will be executed by their native CPUs

Few words about performance

So finally, every time a java program must be interpreted before its execution. We may think that, it may affect the overall performance of any java application, but not!! We will see why?

In case of other languages like C/C++, build (executable code) generated on one machine is copied and executed on another machine as an application, may be with poor performance because executable code is optimized and generated for one processor and is executing on another processor, where as in java, the JVM produces optimized executable code to the local processor, hence will have better performance.

Just In Time (JIT Compiler)

Just In Time Compiler is a part of JVM, used to improve the performance further. The process of translating entire Java byte code into native executable code in one step may take long startup time and suffers from performance.

Just In Time compiler simply compiles each portion of code as it is called (just in time). When code has been compiled once, the resultant native executable is stored in the memory until the application exits. So that, it does not need to be recompiled next time when the same portion of code is called. This process is more efficient than compiling the entire application code at the startup, because the total application may have thousands of lines of code, no need to execute the total application at any given time.

Summary

  • The java compiler compiles the java source code and produces the platform independent byte code
  • The byte code is a highly optimized set of instructions generated to be executed by the JVM
  • Byte code can’t be directly executed by any CPU because not generated according to any platform
  • JVM translates the platform independent byte code into platform dependent executable code according to the native platform
  • Platform dependent executable generated by the JVM is executed by the CPU

Previous post:

Next post: