Why java is platform independent ?

As we hear all the time that the Java is the Platform Independent, then which is the thing that makes java more cooler(platform independent) than the other languages.

when we consider the computer, we know that it understands only one language and that is the Binary and as a programmer what we write is the language other than the Binary, so that to convert our code to the from of the language that the system can understand, at that time the compiler comes in to the picture, the function of the compiler is to convert the source code from the regular human understandable language to the Object language(Binary)

Java is been developed by the sun microsystems which is now been owned by the Oracle Corporation, the Java people thought little bit different from the others, they came with the Consept of the thing called “Class Files” or “Byte Code”, i ll explain that shortly.

Following figure represents the Function of the Compiler with the Regular languages like C,C++ etc.

Process of Compilation in the Regular Languages, that outputs directly in to the Object code
Process of Compilation in the Regular Languages, that outputs directly in to the Object code

as you can see , in the first pass you will be having the code and then you will compile that code with some compiler and than it will result in some object file that can only be understand by your system, object file is nothing but the file that contain ones and zeros. And the Linker is nothing but the one that links your current file to the other files thats been written already.

 

Where as in Java the Follwing thing is goin to happen, when you compile the java code.

java compilation Process which Results into Class file(Byte Code)
java compilation Process which Results into Class file(Byte Code)

So as you can see that , when you wll be having the source code and when you will compile that with any kind of Compiler(Ex:-Eclipse) and that will output in some kind of file that is known as the Class file that consist some kind of numbers that is known as the Byte codes, or some intermediate code, that can only be understand by the JVM(Java Virtual Machine), the class file that wont be understand by the machine, the JVM is going to pretend like that its a machine and that is going to execute that code for you. and so that to Execute the Java programs we are required to install the JRE in our systems that consist the JVM, and to Compile the code we need to have JDK.

so that you might be having the Question that why do we need to do this Extra process of going from class file to the JVM and then the output, then the Answer is that we live in the world where we have the machines like MAC, PC(Windows), Linux etc, and the 1’s and 0’s of the MAC is different from the 1’s and 0’s of the PC(Windows), and that is different from the 1’s and 0’s of Linux. so to deal with this we use this java,  and the Class file will remain same for all the machine, and the JVM will result in the same output for all the machine for that class file, the thing is that JRE is the one that is platform dependent and there are various JRE available for all the Platforms, and thats why the code thats written in the MAC can be executed on the PC with the help of its class file, so that it is known as the paltform independent.

Advertisements

2 thoughts on “Why java is platform independent ?

  1. so i want to ask 1 question that what about any mobile phones to run java applications…? can we hav to install JRE in our mobile to run java applications …?
    and what if i developed a new operating system and now i want to run my java application on that o.s then should i hav to install JRE JDK ?
    thanks in advance …..i am asking this q because in my interview i face this q where i stuck up.

    1. Hi there,

      Regarding your question for any sort of Operating system if you want to run the java code you are required to have JRE within your system, but if you do not want to execute the code and you just want to write/develop the java programs you need to have JDK.

      Hope this will help you.

      Good Luck.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s