Search This Blog


Thursday, August 9, 2012

How Java achieves platform independence

Java is platform independent language How?
Java programming language provides platform independence, what does it mean ? It means same Java program can be run on any platform or operating system e.g. Windows, Linux or Solaris without any change. This is the great benefit for some one coming from platform dependent programming language like C or C++ whose code needs to be ported for every single platform because they use native libraries, which differ in every platform. Now question comes how Java achieve platform independence, what makes Java programs running on every platform without any change ? This is one of the most basis question Java programmer ask when they start learning Java programming language. If you read further you will come to know about class files, bytecode and Java virtual machine which together provides platform independence to Java. One of the simplest analogy I can associate with platform independence is the person taking red carpet with him and instead of walking on floor, he always walks on red carpet, no matter where he is walking. That red carpet is the JVM, your Java program runs on JVM rather on any particular platform or machine. 

Java Compilation and execution

How Java achieves platform indepence For those who don't know Java is both compiler and interpreter language. When you compile a Java program it creates .class file which is collection of byte code, these byte code are not machine instruction instead they are instruction which Java virtual machine can understand. Since every Java program runs on Java virtual machine, same byte code can be run on any platform. key is byte code is not machine instruction they are platform independent instruction to JVM. On the other hand JVM or Java virtual machine is platform dependent because it converts byte code into machine level instruction which is platform specific and that's why you have different version of JDK and JRE for windows and Linux because both JDK and JRE comes with Java virtual machine. if you are confused between JVM, JRE and JDK then read my most on difference between JDK, JRE andJVM in Java.

Byte code is created when you compile Java program using Java compiler "javac" and byte code runs on JVM which is created by running "java" command. In detail when you run "java" command it creates Java virtual machine, loads Main class specified in command line and calls standard main method in java.

In summary combination of byte code and JVM makes Java program platform independent. Write once run everywhere was Java’s mantra when it started ruling programming world in mid and late 90’s. Always remember, Java programs are platform independent but JVM is not. That's why you have different JVM and JRE installation for different platform e.g  Mac, Windows, Linux or Solaris. Similarly their are different JVM for 32-bit and 64-bit machine. 


  1. Life Cycle of JVM :

    When a Java application starts, a runtime instance is born. When the application completes, the instance dies. If you start three Java applications at the same time, on the same computer, using the same concrete implementation, you'll get three Java virtual machine instances. Each Java application runs inside its own Java virtual machine.

    For Example :

    If we have a Node manager which contains 3 Managed server having instance of JVM, then there will be 3 instance of JVM running on managed server, all of them will be pointing to the Node manager.

    The source code is first written in plain text files ending with the .java extension. Those source files are then compiled into .class files by the javac compiler. A .class file does not contain code that is native to your processor; it instead contains bytecodes — the machine language of the Java Virtual Machine (Java VM). The java launcher tool then runs your application with an instance of the Java Virtual Machine.

    Please chek the below UrL for the details :

    This explains what is JVM and how does it work.Very useful info.


Java67 Headline Animator