How Java programs are Platform Independent?
JVM makes the Java platform independent. Java program can be run on any machine that contains JVM because java programs are initially
compiled into byte code language instead of machine level language.
Byte code is read by JVM and converted into machine level code depending on the machine.
That's how JVM makes Java platform independent.
Compiling and running a java program involves below steps :
- Compile Java programs using JAVAC compiler.
- Upon compilation byte code is generated instead of machine specific code.
- Byte code is compiled using JVM which generates the machine specific code.
- Machine code is run on the operating system.
The .class file get is given to the Java Runtime Environment (JRE) which contains Java Virtual Machine (JVM), JVM checks the byte code using Code Verifier then passes to Just-in-time (JIT) compiler to create a binary on a machine.
A number of things happens inside JVM like loading, linking and initialization. Go through the JVM section(JVM Details) to know more.
Following is the Java program execution workflow :
.java → JAVAC Compiler → .class → JVM(Class Loader → Code Verifier → JIT) → Binary Code → Execution