How Is Java Platform Independent?

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

Java is a platform-independent and 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 1995 for SunMicrosystem who released it.

Java_Program
  • 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 Programmed 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 that executes the byte code to obtain the desired output.

What does Platform Independent Means?

What does the platform really mean? A platform is basically 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 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 that 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 a 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. The same memory and resources are used by multiple threads running at the same time.

    For example:
    Computer Games uses multiple threading in access. The 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 is converted into bytecode also known as .class file.
  • Now this byte code is something that makes it special with its features as it is different from machine code which can not be transferred/ portable to another platform while that can be done in the 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 program.
  • 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, recognizes the platform, 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.
java

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

Frequently Asked Questions

Is Java platform-independent if yes why?

Java is a platform independent language. JVM, or Java Virtual Machine is used to identify the platform on which Java is being run. It is JVM which morphs the code into a language a particular platform (machine) will understand.

Why is Java platform-independent and portable?

Java’s compiler converts the code written by the developer into bytecode. This bytecode is utilised by JVM (Java Virtual Machine) and converted into a language the machine on which Java is being run would understand. For this particular reason, that Java is platform-independent it is also portable.

What is platform independence?

Platform independence is the phenomenon by virtue of which languages, frameworks, tool and software do not have to undergo any major changes to be fit to run on different platforms and devices.

Why is C++ not platform independent?

The conversion of code from a C++ compiler to a C++ assembler which converts the code into machine language varies for machines with different architectures and hence C++ is not platform independent.

What is the difference between platform-dependent and independent?

Platform dependent languages, tools and frameworks differ from platform independent languages, tools and frameworks in a way that only platform independent ones can be executed on any machine whereas platform dependent ones have to be modified for them to be executed on different machines depending upon the operating system and machine architecture.

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

Exit mobile version