How is Java platform independent?

How is Java platform independent?
How is Java platform independent?

Java is an object-oriented programming language similar to C++ with some advancement in feature and simplification in use. It was developed by James Gosling with his team including Mike Sheridan and Patrick Naughton in the year of 1995 for SunMicrosystem who released it.

  • It is portable, secured, dynamic, concurrent, multithread and many more with such interesting features.
  • But one feature it supports is PLATFORM INDEPENDENCE. Let’s discuss this feature and its impact in detail.
  • Before this, one should know how a program executes in JAVA.So let’s dive deep into the execution of a program in it.

How a Programme executes in JAVA?

Program execution can be categorized in following steps:

  • Basically, javac is the compiler that compiles the java code.
  • What does this compiler do? So this javac compiler compiles the java code into ByteCode. which is also a .class file.
  • Now, we have JVM (Java Virtual Machine) which is an interpreter which executes the byte code to obtain the desired output.

What does Platform Independent Means?
What does platform really mean?
A platform is basically a software or hardware where a program normally runs. In Java, we have a Java Runtime Environment, which is a platform for this language. Platform Independent means the program execution is independent of the Operating System of the System. “Write Once Run Anywhere (WORA)” is the approach that it follows.

How Platform Independency?
So, now as we talked about a high-level program execution in it. Let’s make it platform-independent.
The byte code generated by the javac compiler can be executed on all kinds of Operating Systems which is unlike other programming languages. Basically, the byte code generated by the Java needs JVM only. It does not depend on the operating system.

Let’s talk about the difference between how a program is written and executed in Java and other programming languages.

What happens in Languages like C++:
Generally, we have a compiler which converts the High level written code to machine code. Now, the question comes why is this done? This is done because what we write as a code is a high-level language that the machine can not understand, thus, the compiler converts the high-level code into machine-executable code, which can be run directly by the CPU.
Now, in languages like C++, the code is converted into the executable code, i.e., .exe file, which
is platform-dependent only. This .exe file can not be run on a different OS. Thus, this does not support platform independence. These languages are platform-dependent only.

What happens in JAVA?
In Java, what the compiler does is convert the program into bytecode which is not an executable code. Now, this bytecode can be executed on any other platform/Operating System. What happens when we run its file or program, we get a .class file which is basically a non-executable file. In order to run a .class file, we would need a virtual machine. JVM is platform dependent for sure. Like it is different for different environments. For example, Windows has a different JVM than MAC or Linux. One important feature that it supports is portability as it is platform-independent by portable the bytecode of its code.

Other Important features that it supports:

  1. Portable:
    It won’t be unfair if we say, Java is byte code portable language. It provides a different level of
    portability like:
    a) Source Code Portability
    b) Operating System or GUI level Portability
    c) CPU architectural portability
  1. OOP:
    It supports object-oriented programming. This feature of Java makes it more extendable as a
    language.
    Some of the concepts that oop has:
    a) Object
    b) Class
    c) Encapsulation
    d) Inheritance
    e) Polymorphism
  1. MultiThreading:
    It is basically the ability to do multiple tasks at the same time. It supports multithreading and thus multiple tasks can be done at the same time in parallel. Its feature helps save memory and resources. Same memory and resources are used by multiple threads running at the same time.
    For example:
    Computer Games uses multiple threading in access. Performance of long-running applications can be improved through multithreading.
  1. Secure:
    When it comes to security, it is one of the most secure languages we have. What makes it
    secure is the way it executes its programs.
    For example:
    Its programmes always run in a Java Runtime Environment (JRE) with almost null interaction
    with the Operating System, thus it is one of the most secure.
  1. Simple:
    It is simple to use. It is very necessary and important to learn Object-Oriented Programming
    in Java. Once it is done, it is quite easy to understand the language.
  1. Performance-wise:
    Performance-wise it is quite good, this is because of the use of Just-In-Time compiler that
    it uses, it makes it one of the high-performance languages.

What Role Compilation and Interpretation Play in Platform In-dependency?

  • It basically uses both compilation and interpretation to achieve platform in-dependency.
  • When a program is compiled into Java, it is not converted into machine code but it is converted into bytecode also known as .class file.
  • Now this byte code is something that makes it special with their features as it is different from machine code which can not be transferred/ portable to other platwork while that can be done in case of Java.
  • Java Virtual Machine that is capable of running that byte code.

Why is Java not Completely Platform Independent?

  • As we know now, High-level code is converted into Machine code in order of execution
    of a programme.
  • Java High-level code is first compiled to bytecode by the javac compiler.
  • Now, this byte code is converted into machine code by Just In Time(JIT)
    Interpreter/Compiler.
  • Now this JVM which has JIT as a compiler is platform dependent.
  • What happens is JVM picks the bytecode, recognises the platwork and passes it to JIT
    which is its compiler to convert the bytecode into machine code. This JIT is platform
    dependent.
  • Thus, it can be said that Java is not completely platform-independent.

Prerequisites to Run JAVA Program:
To run a java program, we must need a JDK(Java Development Kit).
To create any kind of powerful java application, JDK is just sufficient and enough powerful kit contains all the required software.
Let’s see what JDK actually contains:

  • Java Runtime Environment.
  • Javac compiler
  • Java Virtual Machine
  • Document generator
  • Other necessary library and tools

The above picture basically clarifies all about JVM JRE and JDK. Here JVM is a subset of JRE which is again a subset of JDK.
Example:
Let’s see how we add two numbers and what we get as a result.

  • When we write a program we save the program with .java extension.
  • After the compilation by javac compiler, it is converted into .class file.
  • Now, after this JVM executes/interprets the byte code into machine code.
  • We get the desired output.

This is how a simple addition java program looks like:

public class AddTwoNumbers {
public static void main(String[] args) {
int x1 = 5, x2 = 15, sum;
sum = x1 + x2;
System.out.println(“Sum of these numbers: “+sum);
}
}
Output: 20

Conclusion
Now, as we saw that java is platform-independent, this is all because of the ability to generate the bytecode. Forming the intermediate stage, i.e., bytecode makes the java platform independent.

To explore articles on Java, click here.

By Deepak Jain