How Java Is A Platform Independent Language?
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.
The meaning of platform-independent is that the java compiled code(byte code) can run on all operating systems.
A program is written in a language that is a human-readable language. It may contain words, phrases, etc which the machine does not understand. For the source code to be understood by the machine, it needs to be in a language understood by machines, typically a machine-level language. So, here comes the role of a compiler. The compiler converts the high-level language (human language) into a format understood by the machines. Therefore, a compiler is a program that translates the source code for another program from a programming language into executable code.
This executable code may be a sequence of machine instructions that can be executed by the CPU directly, or it may be an intermediate representation that is interpreted by a virtual machine. This intermediate representation in Java is the Java Byte Code.
· What is a Platform, and What is a Platform-Independent Language?
· Compiling a Code
· How Does a Program Execute In JAVA?
· JVM (Java Virtual Machine)
· What Role do Compilation and Interpretation Play in Platform In-dependency?
· How Java Provides Platform Independence?
· Why is Java Not Completely Platform Independent?
· Conclusion
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.
Platform independence refers to the program’s independence from the system’s operating system. It adopts the “Write Once Run Anywhere (WORA)” methodology.
Compiling a Code
Every time we write code, we adhere to a programming language’s syntax, which is generally readable. A few words, variable names, phrases, method names, etc., are included. All of them are regarded as literature that humans can read and understand.
As a result, a computer runs a compiler to turn source code into machine-readable code, also known as machine-level language. The compiler creates the machine-level code in a certain format so that the CPU can easily understand it and execute it without error.
Unless both computers run on the same OS, we cannot run code that has been developed on one machine in another when a machine-level language differs by each OS. Many developers and programmers think this is unacceptable. However, Java simplifies things and includes a fix for this.
How Does a Program Execute In JAVA?
In Java, the compiler converts the program into bytecode, which is not executable code. The JVM (Java Virtual Machine) then executes the byte code at runtime. Java source code is compiled into bytecode when we use the javac compiler.
The resulting bytecode is not machine-executable and any and any other platform or operating system may run it. but it requires an interpreter to run on a computer. When we execute its program or file, we obtain a .class file, which is a non-executable file. The JVM serves as this interpreter, and as a result, it executes the Bytecode. JVM is undoubtedly platform-independent. Similar to how it varies depending on the surroundings. For instance, MAC and Linux have a different JVM than Windows. It supports portability as one of its key features since its code’s portable bytecode makes it platform-independent.
The bytecode gets saved on the disk with the file extension .class . When the program is to be run, the bytecode is converted, using the just-in-time (JIT) compiler. The result is machine code which is then fed to the memory and is executed.
Java code needs to be compiled twice in order to be executed:
- Java programs need to be compiled to bytecode.
- When the bytecode is run, it needs to be converted to machine code.
The Java classes/bytecode are compiled to machine code and loaded into memory by the JVM when needed the first time. This is different from other languages like C/C++ where programs are to be compiled to machine code and linked to create an executable file before it can be executed.
The compiler creates an OS-dependent.exe file when C or C++ is used (these languages are not platform independent). This.exe file won’t run when we try to run it on a different OS since it depends on that OS and isn’t compatible with it.
JVM (Java Virtual Machine)
The JVM, a component of the Java compiler, transforms .java files into.class files. The key function in the source code is called JVM when we compile Java Code.
JVM(Java Virtual Machine) acts as a run-time engine to run Java applications. JVM is the one that actually calls the main method present in a java code. JVM is a part of JRE(Java Runtime Environment).
Java applications are called WORA (Write Once Run Anywhere). This means a programmer can develop Java code on one system and can expect it to run on any other Java-enabled system without any adjustment. This is all possible because of JVM.
When we compile a .java file, .class files(contains byte-code) with the same class names present in .java file are generated by the Java compiler. This .class file goes into various steps when we run it. These steps together describe the whole JVM.
In Java, the main point here is that the JVM depends on the operating system — so if you are running Mac OS X you will have a different JVM than if you are running Windows or some other operating system. This fact can be verified by trying to download the JVM for your particular machine — when trying to download it, you will be given a list of JVMs corresponding to different operating systems, and you will obviously pick whichever JVM is targeted for the operating system that you are running. So we can conclude that JVM is platform-dependent and it is the reason why Java is able to become “Platform Independent”.
Important Points:
- In the case of Java, it is the magic of Bytecode that makes it platform independent.
- This adds to an important feature in the JAVA language termed as portability. Every system has its own JVM which gets installed automatically when the jdk software is installed. For every operating system separate JVM is available which is capable to read the .class file or byte code.
- An important point to be noted is that while JAVA is platform-independent language, the JVM is platform-dependent. Different JVM is designed for different OS and byte code is able to run on different OS.
What Role do Compilation and Interpretation Play in Platform In-dependency?
To accomplish platform independence, it combines both compilation and interpretation. When a programme is compiled into Java, it is turned into bytecode, often known as a.class file, rather than machine code. Now, this bytecode is something that distinguishes it from machine code with its features since it can be moved to/portable to another platform, but Java cannot. That bytecode may be executed on a Java Virtual Machine.
How Java Provides Platform Independence?
In most programming languages, a compiler (or interpreter) generates code that can execute on a specific target machine. For example, if you compile a C++ program on a Windows machine, the executable file can be copied to any other machine but it will only run on other Windows machines but never another machine (e.g., a Mac or a Linux machine). A platform is determined by the target machine (along with its operating system). For earlier languages, language designers needed to create a specialized version of the compiler (or interpreter) for every platform. If you wrote a program that you wanted to make available on multiple platforms, you, as the programmer, would have to do quite a bit of additional work. You would have to create multiple versions of your source code for each platform.
Java succeeded in eliminating the platform issue for high-level programmers (such as you) because it has reorganized the compile-link-execute sequence at an underlying level of the compiler. Details are complicated but, essentially, the designers of the Java language isolated those programming issues which are dependent on the platform and developed low-level means to abstractly refer to these issues. Consequently, the Java compiler doesn’t create an object file, but instead it creates a bytecode file which is, essentially, an object file for a virtual machine. In fact, the Java compiler is often called the JVM compiler (for Java Virtual Machine).
Consequently, you can write a Java program (on any platform) and use the JVM compiler (called javac) to generate a bytecode file (bytecode files use the extension .class). This bytecode file can be used on any platform (that has installed Java). However, bytecode is not an executable file. To execute a bytecode file, you actually need to invoke a Java interpreter (called java). Every platform has its own Java interpreter which will automatically address the platform-specific issues that can no longer be put off. When platform-specific operations are required by the bytecode, the Java interpreter links in appropriate code specific to the platform.
As with interpreted languages, it is possible to get Java programs to run faster by compiling the bytecode into an executable; the disadvantage is that such executables will only work on the platform in which it is created.
Why is Java Not Completely Platform Independent?
Clearly Java cannot run on a platform that doesn’t have a JVM. So in the most literal sense of the word, Java isn’t platform independent. But at the same time, your definition of platform independence isn’t useful. No possible language could ever be truly platform independent, because some aspect of the platform would need to be able to parse the language. And given all possible platforms, some necessarily won’t be able to parse it.
So, given the fact that many, many platforms run a JVM, and that Java code can be run on any JVM with the same meaning, Java is platform-dependent.
Conclusion
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.
Thanks for reading.Happy learning 😄
Do support our publication by following it