How Java is Platform Independent

Anyone who starts learning Java will agree that the first thing they will hear is that Java is Platform Independent language.


For this to understand ,first we need to understand what do we mean by platform.

Platform = Hardware + Operating System

So that means when we say Java is platform independent ,then we mean that code written in Java language can be executed on any of the computer with different  hardware and operating system.
So same java code can run on machine with Windows on it and it will run fine on machine with Linux.

So the next question is how Java achieve platform Independence?

Answer lies here.

When we compile our java code(.java file) using Java compiler,java compiler will convert this java code to byte code in the form of .class file.Normally in other languages like C, the compiled code is the exact set of instructions the CPU requires to execute the program,however in Java, this compiled code is a set of instructions for a virtual CPU which is required to work the same on every physical machine.This virtual CPU or virtual machine we call as Java virtaul machine or JVM.

Since the JVM must translate this bytecode into machine language, and since the machine language depends on the operating system being used, we can say  that  JVM is platform other words, the JVM is not platform independent.So its different JVMs for different operating systems which make Java Platform Independent.There are standard specifications published by Oracle Corporation which each vendor who is implementing JVM needs to follow,such that each JVM would be able to understand the byte code.

Any feedback ,suggestions ,questions on the post are welcome.