Platform Independence in Java

Introduction

 
Java is a platform-independent language. In other words, you can write your code once and then run it anywhere, on any platform that provides the environment to run it. This environment is the Java Virtual Machine (JVM). The JVM should be present to execute the code. The JVM is different for each platform. In the case of Java, platform independence does not mean that you can run the code anywhere; you can run it wherever the environment is provided. This is the key point of platform independence in Java.
 

What is Platform?

 
what is platform 
 
We can define a platform as the sum of hardware and software, that provides the environment to run the program. We can realistically say that a platform is the computer and the operating system running on it. The operating system can be Windows, Mac, Linux, or any of many others. To run a program on any machine you need to convert it to the machine language so that the specific machine can understand it. In Java the source code is not directly converted into the machine code. It is first converted into the byte code of the JVM.
 

Source Code to Byte Code

 
source code to byte code 
 
In Java, the source code is compiled and converted into the byte code. This byte code is not the machine code so the specific platform cannot understand it since it is not machine code.
 

Byte Code to Machine Code

 
source to machine code 
 
In Java, the byte code is not the required machine code. So for the required machine code we have the Java Virtual Machine in Java. It interprets the byte code and converts it into the machine specific code. So the Java Virtual Machine is very important because it makes the byte code understandable to the machine. It is the reason that, if any machine does not have the specific Java Virtual Machine then it cannot understand the byte code and the program does not execute.
 

Platform-independent and Platform Dependent

 
platform independent and dependent 
 
Using the Java Virtual Machine we can make the byte code understandable to any platform. That is why the byte code is known as platform-independent. But on the other hand the Java Virtual Machine is different for each platform; that is why it is known as platform-dependent. Java is platform-independent because it does not depend on any type of platform. Hence, Java is platform-independent language.
 
Important Points
  • In Java, programs are compiled into byte code and that byte code is platform-independent.
  • The byte code is executed by the Java Virtual Machine and the Java Virtual Machine is platform dependent.
  • Java is platform-independent.
  • Any machine to execute the byte code needs the Java Virtual Machine.

Summary

 
This article has explained the platform independence of Java.