How Java is platform indpenedent

Probably the first thing that is written anywhere about java is that it is a platform independent language. Have you ever wondered that what is meant by platform independent? How java is platform independent? Why other programming languages lack this portability? The purpose of this article is to understand you about all these terminologies.

Before going into detail, first you have to understand that what is a platform?

What Is A Platform?

A platform consists of computer hardware and Operating system. For example, a PC with Windows 8.

Platform dependent means the program can be executed only on a specific platform. Many programs like Skype can be used on multiple operating systems like Windows or OS X. but notice that this software has different versions for different operating systems, which means that Skype is not actually platform independent. Basically the programs written in dependent languages require some code changes to run on other platforms. C++ is an example of platform dependent programming language.

Platform independent means the program that we have developed can run on any platform. The code remains the same irrespective of the platform involved. Java is an example of platform independent programming language. One Program written in Java can run on Windows or OS X without any modification.

How Java Is Platform Independent:

The reason why java is platform independent is that it is a structured differently from other programming languages. In fact, Java is both compiler and interpreter based language. In other programming languages like C/C++, the source code you write must be compiled in machine code to execute because computers can only execute machine code. The compiler, which compiles the code, uses the functions of the current operating system to compile the source code. This compiled program just runs on that operating system as its code was translated interacting the functions of specific operating system. So, the program becomes platform dependent.

On the other hand, the java compiler compiles the source code and produces an independent intermediate code called byte code instead of direct machine code. This code is platform independent because java compiler doesn’t interact with the local platform while generating byte code. In fact, this code is represented in hexa-decimal format which is same for every platform. You can say that byte code generated on Windows is same as the byte code generated on Linux for the same java code.

Now this code must be converted into machine code to be executed by a computer, here JVM comes. JVM stands for JAVA Virtual Machine and it is available as a part of JDK & JRE (described later) for different operating systems. The JVM translates the byte code into machine code which can be executed by a computer. This JVM works like a virtual machine on which platform independent byte code runs and this machine produces a platform dependent machine code. No matter, what physical platform you are using, the part where JVM interprets byte code is guaranteed to work in the same way. Since all the JVMs work exactly the same, the same code works exactly the same way without recompiling. So the same code works on all operating systems without modification.

How Java Is Platform Independent, when it requires JVM:

In true sense, Java is NOT PLATFORM INDEPENTENT programming language as it requires JVM to run upon. But it works like a platform independent language with pre-installed JRE/JDK on the computer.

JVM comes as a part of JDK and JRE. JDK stands for Java Development Kit which is used to write, compile, debug and execute Java code. While JRE stands for Java Runtime Environment which is required to run java programs. So, every computer must be preloaded with JRE to run any java application.

Mostly computer manufacturers sell their computers with pre-installed JRE. Now, these computers are familiar with programs written in Java language. So, they run/execute the program without demanding a single file. But, if computer manufacturers have not added JRE in their systems then JRE should be installed manually.

But, wait a minute, think that you want to run a program written in C/C++ (platform dependent code) on another platform then what you would do? Surely you will make some changes in the core code of program. This could be a long and complicated process and you also should have knowledge of the construction of target platform. But, the java program can run on any platform without modifying the code. Just, JVM is required to run the code on any platform exists. Your byte code is same for every platform and JVM converts it into machine code according to local platform. This machine code is then executed by computer.
Author: Kamal Choudhary is a tech geek who writes about c++ programming tutorials on C++ Beginner. He is a student of computer science in University of Gujrat, pakistan. He loves to write about computer programming. You can find his full bio here. Follow him on twitter @ikamalchoudhary


technology 604103841764410426

Post a Comment



Recent Posts


Join Us