How Is The Java Platform Independent?

How Is The Java Platform Independent?
How Is The Java Platform Independent?

Introduction 

Java is an object-oriented programming language developed by Sun Microsystems in 1995. It is platform-independent, which means Java applications can run on any operating system.

Some of the other features of Java are:

  • Object-Oriented: Everything Java is an object, and concepts like inheritance, polymorphism, encapsulation, etc., can be incorporated into the code.
  • Secure: Java is secure as it doesn’t have an explicit pointer. Classloader, Bytecode Verifier, and Security Manager also help in improving security.
  • Robust: The automatic garbage collection in java removes the objects which are not being used by a Java application anymore. It also has strong memory management.
  • High Performance: It is faster than other traditional interpreted programming languages.
  • Multithreaded: Many tasks can be dealt with at once using multiple threads, a separate program executing simultaneously.
  • Distributed: It facilitates users to create distributed applications in Java.
  • Dynamic: It supports the dynamic loading of classes, i.e., classes are loaded on demand.
  • Architecture neutral:  It has no implementation-dependent features.
  • Portable: We can carry Java bytecode to any platform.
  • Platform independent: Java applications can run on any platform.

In this blog, we will discuss the platform independence and portability features of Java in depth.

Programme Execution in Java

Before discussing how Java is platform-independent, we will discuss how a program is executed in Java.

The machines cannot understand the code written in Java. So, first,  the source code is converted into a machine-understandable form. For this, javac, a compiler, converts the High-Level language into a machine-understandable language called the Java byte code.

The java compiler compiles the java file into a .class file.

The byte code is a non-executable code and needs to be interpreted. A virtual machine called Java Virtual Machine(JVM) is used for this. It provides a runtime environment for the byte code to be executed. Without the JVM, one cannot run the java applications on a machine.

JVM consists of a subsystem called classloader, which loads the class files. After that, the Bytecode verifier checks the code fragments for illegal code that can violate access rights to objects. The interpreter then reads the bytecode and executes the instructions. And, finally, the output is obtained.

Runtime Flowchart

Java Virtual Machine

Java Virtual Machine (JVM) is an abstract machine that provides a runtime environment for the bytecode to be executed. It is a part of the Java Runtime Environment (JRE) and is the one that calls the main method in the java programme.

The three aspects of JVM are:

1. Specification: JVM is an abstract machine defined by a specification. Java applications can be run only inside the concrete implementation of the abstract specification of JVM.

2. Implementation: The specifications defined in JVM are implemented, and a runtime environment is created for code execution. The implementation of JVM is known as Java Runtime Environment (JRE). JVM implementations are provided by various companies, both open-source and proprietary. HotSpot, the open-source JVM implementation by Oracle, is one of the most popular ones.

3. Instance: Each time we run a java application, e.g., by running the Java command in the command prompt to run the Java class, an instance of JVM is created. This JVM instance runs the entire programme, and it is lost once the programme ends.

The JVM performs the following main tasks:

  • Provides runtime environment
  • Loads code
  • Verifies code
  • Executes code

How is Java Platform Independent?

A language is called platform independent if the applications written in that language can run on any operating system (OS). Java, the Python, C#, Scala, etc., are examples of platform-independent programming languages.

In Java, the code is compiled by the compiler and converted into platform-independent bytecode. This byte code can run on any platform, making Java Write Once and Run Anywhere (WORA) language. Hence, java applications can be executed on any platform.

Languages like C++ and C do not generate intermediate code like Java. Instead, they directly generate the machine code, which is specific to one platform. This compiled code of languages can only be understood and executed by the operating system in which it was compiled.

This compiled code cannot run on any other platform, which makes it a platform-dependent language. In the case of Java, the JVM converts the bytecode to a machine code which makes it platform-independent.

Is JVM platform-dependent / platform independent?

JVM is platform-dependent even though Java is platform-independent. It requires it to be installed in the system before running the java program. When you try downloading JVM for your machine, you will be offered a list of JVMs corresponding to different operating systems. 

JVM acts as an interface between the operating system and java language. It provides an environment to execute the .class file. In the end, this depends on the kernel, which is different for different operating systems. Hence, different JVMs are designed for different operating systems.

And as a matter of fact, JVM is a platform-dependent virtual machine that enables Java to be platform-independent.

How is Java portable?

Portability refers to an application’s ability to run on a different platform than it was designed for. Java applications can run on any machine without recompilation or tweaking the code. 

Java has a portable bytecode. Once you’ve compiled your Java source to bytecode (.class file), that file can be used on any Java-supported platform without modification. Every platform, whether it is Windows, Linux, etc., can execute the bytecode (.class file) with the help of the JVM. Hence, the bytecode makes Java portable in nature.

Frequently Asked Questions

Why is Java platform-independent and portable?

Java is platform-independent as the Java applications can run on any operating environment. And it is portable as it facilitates us to carry the bytecode to any other platform.

Why is Java called machine independent?

Java applications are not dependent on any property of a particular machine and can be used on any machine. Hence, it is called machine-independent language.

What is meant by platform-independent?

A language is called platform independent if the applications written in that language can run on any operating environment.

What is the difference between platform-dependent and independent?

If a language is platform-dependent, the application will only run on a certain operating system. Eg: C,C++, etc. And if a language is platform-independent, the application will run on diverse operating systems. E.g., Java, Python, etc.

Is C++ platform independent?

No, it’s platform-dependent. The .exe file generated by the compiler is platform-dependent and is not compatible with other Operating Systems.

Key Takeaways

In this blog, we ran you through the following:

  • Features of Java
  • What do you mean by platform-independent language?
  • How is the Java platform independent?
  • What is a Java Virtual Machine?
  • Programme execution in Java
  • Is the JVM platform-independent/dependent?
  • Why is Java portable?
  • Some common question related to platform-independent and portability feature

With this discussion, this blog attempted to give you an idea of why java is platform-independent and portable.

By Hari Sapna Nair