Java has been garnering increasing popularity ever since its launch in the year 1995. With its cool, efficient, easier and faster features, it is now one of the go-to programming languages for beginners and experts alike.
But there is one feature in Java that sets it apart from its equivalents like C++. What is that, you ask?
It is that Java is platform-independent in nature. Why is the Java platform independent?
We will answer all your whatsapp, hows, and whys in this article, so read till the end to get the haze of those doubts cleared. Let us get started!
What is a Platform?
Now, the first question you may have is that what even is a platform? A platform, in the simplest words, is an environment in which a program runs. This environment consists of the software, the hardware and the operating system.
The combined environment of the software, the hardware and the operating system where a program runs is called a platform.
What is Platform Independent?
Different computer programs run differently in different environments. For example, a windows application or software cannot run on a Mac Operating System.
Although there are applications that work on more than one operating system, like MS PowerPoint, they require different programming appropriate for each system.
But with Java being platform-independent, this problem is solved. Platform independent means that software can run in various computer environments with the same programming.
So you do have to change your code for every platform. This feature has the potential to halve the labor of a coder.
Why is the Java platform independent?
The reason why the java platform is independent is that a program written on Java can be run on various platforms without needing to re-write them for each and every platform. This feature of Java runs on the slogan Write Once, Run Anywhere [WORA]. Following is a breakdown of the whole process that makes the Java platform independent.
How is the Java platform independent?
Here we will discuss the process that makes the Java platform independent. To understand that, we need to first understand the compilation process of both platform-dependent and platform-independent programming software.
Whenever we write a program, it contains words, phrases, names etc., that cannot be understood by a computer.
The language we write the code in is known as source code which is comprehensible to a human being but not to computers. This code needs to be converted into a machine language code that can be executed and understood by a computer.
This conversion from source code to machine language code is done with the help of a compiler, and the conversion process is known as compilation. It can also be done with the help of an online java compiler. This machine code is different for different platforms.
Platform dependent Compilation
First, the code is converted into machine language code in platform-dependent software, say, for example, C++ in Windows. The file generated here would be a .exe file. This native code will only execute in a Windows OS and is different for every OS.
So a file generated in a platform-dependent programming language will only run on the OS it is generated in. Meaning a file generated in Windows will not run on mac OS or Linux etc.
Platform Independent Compilation
Here we will break down the compilation and execution process in Java, a platform-independent software.
Java does not compile a program into native code. Instead, it has a special compiler named Javac. You can also use an online java compiler instead of Javac. The compilation of source code creates a different format called bytecode. This code is saved as a .class file.
But this byte code is the same on every platform and thus cannot be executed directly. It needs to be translated to be made executable. This translator is JVM [Java Virtual Machine]. JVM is a Java translator that intakes the byte code and translates it into the output. With the help of JVM, the code is finally turned into a native machine-specific code that can be executed as per the OS you are running.
To sum up, the process goes in the following order:
Source code – byte code – JVM [different for different OS] – the output [Same for all platforms]
We can say that the reason why the Java platform is independent is mainly because of JVM. But is JVM itself platform-independent?
Let us find out to know in-depth of complete independence of Java platform
Is Java completely platform-independent?
We will have to say no to that. Although the byte code is platform-independent, JVM is completely platform-dependent because it translates byte code into native code that can be executed by the system.
JVM translates code according to the platform it is working on. For example, a code translated by JVM on Windows OS cannot be executed in macOS and vice versa. Nevertheless, the output stays the same.
Hence, we can say that Java is not completely platform-independent.
JVM architecture
JVM is a part of JRE. JRE stands for Java Runtime Environment. It is an engine that runs Java applications. The Java Virtual machine cannot understand the source code written by a human being; it deals with computer-specific language.
JVM converts a .java file into a .class file which is composed of byte codes. This .class file has to undergo a series of processes when it is run. This series of processes is what we call the JVM architecture.
Conclusion
Looks like you got the answer to the why is Java platform independent question. Java is platform-independent because a program run on it can be executed on different platforms easily.
We discussed what a platform is and what is meant by platform independent. We also looked over the process of the execution of a code in both platform-dependent and platform-independent software.
Hope you now got better insight into the concept alongside online java compilers!
Top comments (1)
well explained! 👏