Without wasting any time, we will jump to the core understanding of why or how java is platform-independent.
But please please read the full article as I have 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 get executed and run for high-level programming languages like Java, C, or C++.
We will begin with C/C++ that is how they are compiled and gets executed for various machines and what makes the 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 that 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 program 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.
Table of Contents
This is how the C/C++ file gets executed.
From the above image, it is clear that when a code is compiled on a Windows machine it creates windows executable file that is .exe. likewise, 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 its own compiler? It is this unique compiler that makes the C/C++ platform dependent.
Java a platform-independent language.
Java is also a 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 the java compiler also known as ‘Javac’. What it does is it takes a .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 an 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 a million devices out there, then for each machine, there is a unique JVM.
Once the byte code is accepted by JVM which already understands its language then JVM starts its job. As told earlier it is also known as an interpreter, so JVM starts interpreting the byte code according to respective machines. Now interpreter for different machines is 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 the 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 the 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 the respective platform and there you go.
This feature makes java a platform-independent language or you can say java is a portable language.
Till this point, we saw java, why it’s 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 the interpretation of byte code for various machine and for each unique machine there is a unique JVM
- JVM is platform dependent which makes the source code platform-independent.
- JVM comes under the hood JRE (Java Runtime Environment)
Java Runtime Environment (JDK).
- JRE is the environment within which the JVM runs.
- JRE contains JVM, class libraries, and other files excluding development tools such as compiler and debugger.
- JRE is a software layer that runs on top of the operating system.
- JRE also helps java to become platform-independent.
- You can download JRE in an isolated way and you can run code in JRE but you can’t develop and compile the code in JRE.
- Below the whole blue color image indicates JRE.
Java Development Kit (JDK).
- JDK is the key and most important element in developing java applications at its heart lies a compiler.
- JDK allows developers everything from the 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 that contains Compiler, debugger, tools, JVM, JRE everything.
- Below red image below represents the JDK.
JVM (Java Virtual Machine).
JVM is a virtual machine that resides in the real machine that is your computer, and the best part is that the machine language for JVM is byte code.
Now javac never converts your source code to a machine-understandable language, what it does is converts the .java file to byte code, which JVM already understands.
Therefore, the java compiler gives byte code to JVM (which already understands the byte code of the machine) and thus produces outputs.
If the java compiler starts converting each .java file or says source code into machine understandable language then it would be very difficult, because there are millions of machines.
Now for each operating system, there is a unique JVM.
Therefore JVM is the one who makes Java a platform-independent programming language.
The below image will clarify the overall concepts.