Did u know why Java is very much popular? It’s because Java has a platform Independent feature. So let’s discuss why Java is platform independent.
Actually the fact is Java platform independent then but JVM (Java Virtual Machine) is platform dependent.
There are two tools which use for compiling and running Java programs
javac -It is a compiler which converts Java source code to byte code that is a .class file. This byte code is standard for all platforms, machines or operating systems.
Java – It is an interpreter. This interprets the .class file based on a particular platform and executes them.
Jvm -Java virtual machine comes into play. Jvm for windows will be different from Jvm for Solaris or Linux. But all the Jvm take the same byte code and executes them in that platform.
Source code is compiled by the Java compiler (javac) and it converts the source into universal byte code.
Then a universal byte code is read by the Jvm and JVM rum this code on a particular machine.
There can be any number of Jvm but they all understand the one common language called byte code and translates them into the binary executable of a particular platform for which they are developed. There is separate development section in Sun Microsystems to develop the Jvm for a particular platform. For each upgrade of the Jdk, the Jvm for each platform is updated.
Since this is frankly impossible across processor architectures Java is compiled to an intermediate form called byte-code. A Java program never really executes natively on the host machine. Rather a special native program called the Java interpreter reads the byte code and executes the corresponding native machine instructions. Thus to port Java programs to a new platform all that is needed is to port the interpreter and some of the library routines. Even the compiler is written in Java. The byte codes are precisely defined, and remain the same on all platforms.
The second important part of making Java cross-platform is the elimination of undefined or architecture dependent constructs. Integers are always four bytes long, and floating point variables follow the IEEE 754 standard for computer arithmetic exactly.
You don't have to worry that the meaning of an integer is going to change if you move from a Pentium to any other architecture. In Java everything is guaranteed. However the virtual machine itself and some parts of the class library must be written in native code. These are not always as easy or as quick to port as pure Java programs.
I hope now you all can understand that why Java is a platform independent and how a program gets executed in Java.