Without wasting any time, we will jump to the core understanding of why or how java is platform independent.
But please please read full article as I have a great experience in java and had read almost 50+ articles about java platform independent, what is Javac, what is java virtual machine (JVM), what is Java Runtime Environment (JRE), what is Java Development Kit (JDK).
First, we will see how programs gets executed and run for high level programming language like java, C or C++.
We will begin with C/C++ that is how they are compiled and gets executed for various machine and what makes C/C++ platform or machine dependent and then we will see how java is compiled and gets executed and what makes java platform or machine independent.
C/C++ is a high-level programming language which first needs to be compiled by their respective compiler which produces respective .c/.cpp file which can be executed on the platform where these codes are written.
Meaning if you write a programme using C/C++ on windows and compiled it there itself it and created a .exe file that is executable code then it will only and only available for that particular machine.
This is how C/C++ file gets executed.
From the above image it is clear that when a code is compiled on a windows machine it creates a windows executable file that is .exe. like wise if the code was compiled on Linux or macOS it would have created respective executable code .SH for Linux and Mach-O for macOS.
So, did you see how each operating system has their own compiler. It is this unique compiler which make C/C++ platform dependent.
Java a platform independent language.
Java is also high-level programming language, a high-level programming language cannot be run directly on any machine, first it needs to be converted into machine level language and this is achieved by java compiler also known as ‘Javac’. What it does is it takes .java file containing source code and translates it into machine understanding language mainly known as byte code or .class file.
Now this byte code or .class file is pushed into Java Virtual Machine (JVM). JVM is a virtual machine (also known as interpreter) that resides in the real machine that is your computer. And the only language that JVM understands is machine level language in this case byte code. Now for each unique machine there is a unique JVM so if there are million devices out there, then for each machine there is a unique JVM.
Once byte code is accepted by JVM which already understand its language then JVM start its job. As told earlier it is also known as interpreter, so JVM starts interpreting the byte code according to respective machines. Now interpreter for different machines are different. By this time, it would have been cleared to you it is JVM which makes Java a platform independent. If not below image would.
This is how java file gets executed.
The above image clearly tells us that how java file is executed, you can also see that you can write code on various platform but the output will be same, meaning if you write some sorting problem on windows then you don’t need to do any changes in order to run it on macOS or Linux all you need to do is simply execute the code on respective platform and there you go.
This feature makes java a platform independent language or you can say java is portable language.
Till this point we saw java, why its platform independent, and makes it so. Now we will see what is JVM, JRE & JDK.
Java Virtual Machine (JDK).
- JVM is an abstract (abstract means being an idea or not having a physical existence) machine that resides on your real machine.
- JVM provides interpretation of byte code for various machine and for each unique machine there is a unique JVM
- JVM is platform dependent which makes source code platform independent.
- JVM comes under the hood JRE (Java Runtime Environment)
Java Runtime Environment (JDK).
- JRE is environment within which the JVM runs.
- JRE contains JVM, class libraries and other files excluding development tools such as compiler and debugger.
- JRE is software layer that runs on top of the operating system.
- JRE also helps java to become platform independent.
- You can download JRE in isolated way and you can run code in JRE but you can’t develop and compile the code in JRE.
- Below the whole blue colour image indicates JRE.
Java Development kit (JDK).
- JDK is the key and most important element is developing java applications at its heart lies a compiler.
- JDK allows developer everything from compiler, debugger, development tools, JVM, JRE everything.
- Often developers get confused in JRE and JDK. JDK is a package of tools for developing java-based software, and JRE is the package of tools for running java code.
- JDK is a fully completed software for java developers which contains Compiler, debugger, tools, JVM, JRE everything.
- Below red image represents the JDK.