Before Knowing how the Platform Independence is achieved in Java, let us see what the platform Independence mean.
What?
Platform in the term “Platform Independence” means the Operating System.
By saying Java is a platform independent language; it means the Java software that is used in one operating system can also be used in another operating system without any change to the software. For example, a Java app that runs in windows machine will run fine in Solaris machine and also work in a Mobile without any minor change to the Java software.
Here, with this independence, the programmer develops and builds the source code just once and the same compiled files can be used across platforms (Operating Systems). This was a big breakthrough when Java first came to the market and made it unique among other programming languages of that time(C and C++)
JVM is Java Virtual Machine in which the Java source code runs. To run a Java program in any Operating System, we need to have a JVM. For example, if we want to run a Java program on a Windows Operating System machine, we need to have a Windows JVM installed on that particular PC. In the same way, if I want to run the Java Program on a Solaris machine, I need to have Solaris Compatible JVM and the same is with the Mobile too. If I want to run a Java app on a mobile, I need to install the mobile operating systems compatible JVM.
Once the JVM is installed the same Java program can be run in any of those operating systems. The same Java program works in all platforms the same way. Here if you observe, we have different JVMs for different operating systems but the same Java program for all the operating systems.
To conclude this,
JVM is platform dependant, meaning the JVM is different for different platforms.
Java is platform Independent, meaning the Java source code is same among different platforms.
Many often confuse between JVM and Java and think JVM is even platform Independent. I want to stress on this particular thing that JVM is not platform Independent. It depends on platform. We need to install the compatible JVM in any operating system. For windows, we have Windows JVM, for Linux we have Linux JVM, For Solaris, we have Solaris JVM making the JVM Platform DEPENDENT. But, we use the same Java source code in all the operating systems making Java Platform INDEPENDENT.
How?
Now that we understand that Java is a platform Independent language, we can see now how it was made it that way.
Every operating system takes inputs in the form of Binary(0’s and 1’s).
Let us assume that 100100 represent the word “Hero” in windows operating system. This pattern may not be the same in other operating systems. Let us say, the word “Hero” is represented by “0010” in another operating system (say Solaris).
A Compiler is the one which converts the programming language to machine understandable language(0’s and 1’s).
A C compiler validates and converts the C language to machine understandable language(0’s and 1’s).If we write a Program in ‘C’ to print the word “Hero”. First we need to compile the C program using a C Windows Compiler which gives us the output as 100100. The same program when compiled in Solaris compiler will give us 0010.So once we run this compiled code in windows and Solaris operating systems respectively, we get the desired output. But the drawback of this approach is that the code is compiled 2 times. Once for the Windows OS and once again for the Solaris which is not desirable.
To remove this compiler dependency, the Java compiler is made in such a way that it does not output 0’s and 1’s directly. Instead it generated the byte code. This byte code when run in any JVM is converted to machine understandable language(0’s and 1’s)
So with Java, we write the program and build(compile) it. Once and we get the byte code as a output from the compiler, we run it in Windows JVM which gives the output as 100100 and the same byte code when run in Solaris JVM, gives the output as 0010.So in Java, we are compiling the code only once and using the same complied code among different operating systems(platforms) making it a Platform Independent language.
No comments:
Post a Comment