Why Java is Platform Independent?
Java is one of the most famous and extensively used programming languages. It is a platform-independent programming language. Java doesn’t require the entire code to be rewritten for all the different platforms. It supports platform independence using Java bytecode and Java Virtual Machine. Java compiler javac converts the program code into byte code. This byte code is platform-independent and can run on any JVM operating system. JVM interprets the byte code to machine code, and the program is executed.
Computer programs are very closely related to specific hardware and operating system. For instance, Windows applications cannot run on Mac Operating System. Mac applications cannot run on the Android Operating system, and so on.
Some applications are common to all operating systems, like MS Word, so should that be written separately for all the Operating Systems?
To tackle this diversity of platforms, such applications should be written independently for all platforms. Java provides a way to write applications independent of the hardware and operating systems. Thus, it is called a platform-independent programming language.
What is a Platform, and What is a Platform-Independent Language?
The platform can be defined as a distinct combination of hardware, operating system, and software that provides an environment to run programs.
Java is called Platform Independent because programs written in Java can be run on multiple platforms without re-writing them individually for a particular platform, i.e., Write Once Run Anywhere (WORA).
How Java Provides Platform Independence?
To understand how Java facilitates platform independence, let us differentiate between the compilation process of other programming languages, mainly C/C++, and that of Java.
The program written by the programmer, known as source code, is not understood by the computer. Source code is very similar to human language, consisting of words and phrases. It has to be converted into machine language code, which computers can easily understand and execute.
Machine language code is a set of instructions to be executed by the computer. The compiler does this conversion. The process of converting source code into machine code is called compilation. Machine Language Code is unique for different platforms.
Ordinary Compilation Process
The code is first converted into machine-readable language when the C/C++ program is written and compiled in Windows OS. The compilation process will generate natively executable code as a .exe file. This natively generated code runs only on Windows Operating System.
This file cannot run on Mac, Unix, or other Operating Systems as the machine code is different for different Operating Systems. Thus, C/C++ is platform dependent programming language.
Step-by-Step Execution of Java Programs
Contrary to other compilers, the Java compiler doesn't produce native executable files or code for a particular platform. The compilation process in Java instead generates a special format called byte code. This generated file is also referred to as a .class file.
The Byte Code of Java is a set of machine instructions for a Java processor chip called Java Virtual Machine (JVM). Java Byte Code is very similar to machine language, but unlike machine language, Java byte code is absolutely the same on every platform. The byte code is not directly executable on any platform. Java code compiled into byte code still needs an interpreter to execute them on the platform.
Java Virtual Machine or JVM is a special Java Interpreter. Java Virtual Machine takes byte code as input. It interprets and executes the byte code and provides the output of the program.
Note: A compiler translates the entire program at once into machine-readable code. An Interpreter also converts the program to machine code, but it does so by translating it instruction-by-instruction or line-by-line.
The interpreter then converts the byte code into the native code. Native code is just like machine language code, which can be compiled to run with a particular processor and its set of instructions. Thus, native code can be executed by an individual operating system. Java Byte Code is platform-independent but natively executable code generated from byte code using an interpreter is highly platform-dependent and cannot run on other platforms.
For example, source code written in windows OS is compiled by the javac compiler of windows, and the .class file (byte code) is created. JVM of Unix, MAC, or any other platform can interpret this byte code.
Hence, the JVM of that specific platform interprets the byte code and generates natively executable code for the platform. This native code is readily understood by the system and is executed, and it prints Hello World!
Using byte code, a natively executable code can be generated on any platform using a JVM interpreter, irrespective of the platform on which the byte code was created. But the natively executable code of one platform cannot be used for other platforms, i.e., the native code of Unix cannot run on Windows or MAC.
Note: Java code is both compiled and interpreted language. The source code written in Java is first compiled into bytecode. The Java Virtual Machine then interprets the generated bytecode for execution. However, the JVM also makes use of a Just in Time compiler during runtime to improve performances.
Why is Java Not Completely Platform Independent?
Java is not completely Platform Independent. The javac compiler first compiles the High-Level program code written by the programmer, and byte code is formed. This byte code is platform-independent but requires a Just In Time (JIT) interpreter/compiler. JVM, which has JIT, interprets/compiles the byte code, converts it into machine code, and executes the program.
This Interpreter, Java Virtual Machine, is platform-dependent. Different systems have different JVMs. For example, MAC OS has a different JVM, and Windows will have a different JVM. This JVM is capable of reading the .class file or byte code.
So, we can conclude that Java is independent of the platform using Java Byte Code. But the execution of Byte Code on any platform relies on Java Virtual Machine, which is platform-specific.
JVM is part of the Java Runtime Environment (JRE) and acts as a runtime engine to run Java applications. The machine does not directly understand the code written by the programmer, so it needs a compiler to convert the source code into machine language code.
When we compile a .java file, the Java compiler generates .class files (containing bytecode) with the same class name (present in the .java file). This .class file goes under various processes when we run it. These steps together describe the whole JVM Architecture.
Important Features Supported by Java
- Write once Run Anywhere - Java is a very portable language. Once written, it can be carried to any platform.
- Security - The reason behind Java's secure design is the elimination of explicit pointers and Java runtime environment with almost null interaction with system OS.
- Classloader in Java is a part of the JRE (Java Runtime Environment). It loads Java classes into the JVM dynamically. Classloader adds security by separating the packages for the classes of the local file system and those imported from network sources.
- Bytecode Verifier verifies the code fragments for illegal code that can violate access rights to objects. Security Manager determines what files a class can access, such as reading and writing to the local disk file.
- Robust - Java is a Strong Language.
- It has a very accurate automatic garbage collection which runs on the Java Virtual Machine (JVM) to remove objects from memory that is not being used by a Java application anymore.
- Java has strong memory handling capabilities.
- Java supports exception handling and types-checking mechanisms. All these features make it a strong and stable programming language.
- Object-Oriented Programming - Java is an Object-Oriented programming (OOP) language. Some of the features of OOPs are Object, Class, Inheritance, Polymorphism, Abstraction, and Encapsulation.
- Multithreading - Java facilitates multithreaded programming, i.e., it supports multiple operations simultaneously. The advantage of multithreading is that it utilizes the same memory space and other resources to execute multiple tasks simultaneously.
Important Points to Remember
- The main motive behind creating Java Programming Language was to make it a portable, simple, and secure programming language.
- Apart from these, it also supports some excellent features which contribute to the popularity of Java, like Object-Oriented programming, Platform independence, Portability, Dynamic in nature, Architecture neutral, Multithreaded, Robust, Interpreted, etc.
- Java provides Platform Independence by making use of Java Byte Code. Java Byte Code or .class file is generated during the compilation of the code. This Byte Code is platform-independent and can run on any system regardless of the platform it is built upon.
- Byte Code differs from machine code which cannot be transferred to another platform. And Java Virtual Machine, an Interpreter, can run the Byte Code.
- JVM recognizes the platform and converts the Byte Code into machine code which can finally be read and executed.
- Java is a platform-independent language, meaning we run the same code on multiple platforms.
- Java achieves this using JVM and Byte Code. Java compiler converts the programming code into byte code. Byte code is platform-independent and can be run on any processor or system.
- This Java byte code is further interpreted using the JVM of that particular Operating System, and natively executable code is generated. The computer understands this native code, and the desired program is executed.