Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
What is a Platform, and What is a Platform-Independent Language?
3.
What Does Platform Independence Mean?
4.
How Java Provides Platform Independence?
5.
How is a JAVA Code Executed?
6.
Step-by-Step Execution of Java Programs
7.
JVM Architecture
8.
Memory Areas in JVM
9.
Important Features Supported by Java
10.
Why is Java Not Completely Platform Independent?
11.
Why Java is platform-independent but JVM is platform dependent?
12.
Frequently Asked Questions
12.1.
Why is Java platform-independent and portable?
12.2.
What is the difference between platform-dependent and independent?
12.3.
What is the advantage of platform independence?
13.
Conclusion
Last Updated: Mar 27, 2024
Easy

How and Why is Java Platform Independent?

Author Ravi Khorwal
0 upvote
gp-icon
Data structures & algorithms (Beginner to Intermediate)
Free guided path
13 chapters
99+ problems
gp-badge
Earn badges and level up

Introduction

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. This article will teach you how and why is Java platform independent.

why is java platform independent

 

  • 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.
Java platform

Java_Program

What is a Platform, and What is a Platform-Independent Language?

The hardware and software environment on which a program or application runs is referred to as a platform. It contains the libraries, operating systems, and other dependencies needed for the program to run.

Platform-independent languages, or cross-platform languages, can run on many hardware architectures and operating systems without the need for significant recompilation or change. With little to no coding modifications, programs created in a platform-independent language can run on a variety of platforms.

Java, Python, C#, and JavaScript are a few examples of platform-independent languages. These languages include libraries and frameworks that make it easier to write cross-platform software. This allows programmers to create programs that can operate on several operating systems, including Windows, Linux, macOS, and others.
Managed code is another term for platform-independent code. ASP and Java are two popular platform-independent languages.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

What Does Platform Independence Mean?

The ability of a programming language to run on different operating systems without having significant adjustments is referred to as platform independence. Platform independence is achieved when code created in a certain language may be executed on numerous operating systems after being copied and ran without errors.
For example, if you develop code on Windows and it can be executed on another Operating System, such as Linux, without problems, it is deemed platform-independent. This code is also compatible with other operating systems such as macOS, Unix, and Solaris.

How Java Provides Platform Independence?

Java is platform-independent because it uses a virtual machine. Java code is compiled into bytecodes. These platform-neutral bytecodes are independent of operating systems and hardware architectures. The JVM, which executes Java programs, allows platform independence. The JVM interprets or JIT compiles bytecodes into machine code for the operating system. Java uses the JVM to compile code once for all hardware architectures and operating systems. 

The virtual machine bridges bytecodes to any JVM-compatible system. Developers can write code once and run it on multiple platforms without recompilation. This method provides a consistent programming environment by abstracting the hardware and operating system. Java programs behave uniformly across platforms, making them easy to design and deploy. The Java virtual machine handles hardware-related details, relieving developers from platform-specific compilation, and reducing run-time requirements.
The Java Virtual Machine and bytecode compilation make Java platform independent. The JVM executes platform-neutral bytecodes from the compilation. This potent combination lets Java programs run across operating systems and hardware architectures, simplifying development. 

Also see,  Eclipse ide for Java Developers

How is a JAVA Code Executed?

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

Step-by-Step Execution of Java Programs

When executing a Java program, the following steps are involved:

  • Compilation: When a program is written in Java, it must be compiled using the Javac command. The Java compiler (javac) reads the source code (.java files) and translates it into bytecode. The resulting bytecode is stored in a .class file.
     
  • Bytecode Generation: The Java compiler does not produce machine-native code, unlike compilers for other languages. Instead, it generates bytecode, a low-level representation of the program. Bytecode is a set of instructions that can be executed by a Java Virtual Machine (JVM).
     
  • JVM Interpretation: The bytecode generated by the compiler is not directly executable by the operating system. It requires an interpreter to execute the instructions. This interpreter is known as the Java Virtual Machine (JVM). The JVM reads the bytecode, interprets each instruction, and executes them accordingly.
     
  • Just-in-Time (JIT) Compilation: The JVM may employ a Just-in-Time (JIT) compiler to improve performance. The JIT compiler dynamically analyzes the bytecode as it is being executed and can translate frequently executed portions into machine code. This translated machine code can then be executed directly by the processor, resulting in faster execution.
     
  • Program Execution: With the JVM interpreting or JIT-compiling the bytecode, the Java program begins execution. The JVM follows the sequence of instructions specified in the bytecode, performing calculations, manipulating data, and executing method calls as directed by the program's logic.
     
  • Output and Termination: As the program runs, it produces output according to its design and logic. This output may be displayed on the console, written to files, or communicated through network connections. The program continues executing until it reaches its end or encounters an explicit termination statement, such as System.exit(). At this point, the program terminates, and control is returned to the operating system.

JVM Architecture

The JVM consists of several key components:

jvm architecture
  • Classloader: The Classloader subsystem loads Java class files into the JVM. It locates and reads the bytecode from the class files and creates corresponding Class objects in memory. The Classloader performs tasks like bytecode verification, resolution of dependencies, and class linking.
     
  • Runtime Data Areas: The JVM divides memory into several runtime data areas, each serving a specific purpose. We'll discuss more about it in the next section.
     
  • Execution Engine: The Execution Engine is responsible for executing the instructions of the bytecode. It consists of two primary components: Interpreter and Just-in-Time (JIT) Compiler.
     
  • Native Method Interface (JNI): The JNI allows Java programs to call and be called by native (non-Java) applications or libraries. It allows Java to interact with platform-specific functionality or leverage existing native code.
     
  • Native Method Libraries: The JVM includes native libraries that provide low-level functionality and bridge the gap between the JVM and the underlying operating system. These libraries handle I/O operations, network communication, and platform-specific operations.

Memory Areas in JVM

The memory in the Java Virtual Machine (JVM) is organized into multiple distinct areas, each serving a specific purpose. These memory areas include:

  • Method Area: The Method Area, also known as the Class Area, is responsible for storing the bytecode of classes, including information about methods, variables, and constant pools. It is shared among all threads and is where the JVM loads and initializes class-related data.
     
  • Heap: The Heap is the runtime data area where objects are dynamically allocated. It is divided into two main parts: the young generation and the old generation. The young generation is further divided into Eden space, and Survivor space (S0 and S1). New objects are allocated in the Eden space, and those that survive garbage collection are moved to the Survivor spaces. Objects that have been in the Survivor spaces for a certain number of garbage collection cycles are promoted to the old generation.
     
  • Java Stack: Each Java thread has its own stack, known as the Java stack. It is used to store local variables, method arguments, and partial results during method invocations. Each time a method is called, a new frame is pushed onto the stack, and it is popped off when the method completes.
     
  • PC Registers: The PC (Program Counter) Registers contain the addresses of the instructions being executed by each thread. It keeps track of the current execution point within the method.
    Native Method Stacks: Similar to the Java stack, the Native Method Stacks are used for executing native methods, which are methods implemented in languages other than Java, such as C or C++. They store information related to native method invocations.

Important Features Supported by Java

  • 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
     
  • OOP: It supports object-oriented programming. This feature of Java makes it more extendable as a language.
    Some of the concepts that oop have:
    a) Object
    b) Class
    c) Encapsulation
    d) Inheritance
    e) Polymorphism
     
  • 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.
     
  • 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.
     
  • 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.
     
  • 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.

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 C Compiler

java

Why Java is platform-independent but JVM is platform dependent?

Java is platform-independent because it uses a "Write Once, Run Anywhere" approach. Java source code is compiled into bytecode, which is platform-neutral. This bytecode can be executed on any platform that has a Java Virtual Machine (JVM) compatible with that bytecode.

The JVM, on the other hand, is platform-dependent because it is responsible for translating and executing the bytecode on a specific operating system and hardware combination. Each platform (e.g., Windows, macOS, Linux) requires its own JVM implementation compatible with its unique architecture and system calls.

Frequently Asked Questions

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 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.

What is the advantage of platform independence?

Platform independence allows the software to be developed and deployed on multiple operating systems and hardware architectures without modification, enabling greater flexibility, accessibility, and cost-effectiveness for software development and distribution.

Conclusion

In this article, you learned exactly why Java is platform-independent. We discussed how Java code executes, what is the architecture of the JVM, and some important features of Java.

Related links: 

Happy Learning!

Guided path
Free
gridgp-icon
Data structures & algorithms (Beginner to Intermediate)
13 chapters
109+ Problems
gp-badge
Earn badges and level up
Live masterclass