How Java Is Platform Independent
We have heard a lot of times that java is platform independent and majority of other languages are platform dependent. In this article , I will explain in simple terms how java code is platform independent.
Meaning of platform independent:-
A language is platform independent if it can be compiled on a machine and that compiled code can be executed on any computer irrespective of the underlying operating system present in that computer.
Some Examples:-
Java is platform independent because when we compile a .java file using javac a .class file is generated. This .class file can be exported to any computer and it will run as expected (note : you need not to export the .java file) .
C is not platform independent because when we compile C it generates a .exe file which will run on the computer on which it was compiled, but if you export that file to another computer having a different operating system that .exe file won't work, i.e., if you compile a C program in windows it won't work in linux or mac based operating systems.
Why C can't be platform independent:-
When we compile a C code the resultant .exe file communicates directly with the Operating system using system calls to get resources in the machine and to execute instructions. Since different kernels (linux,mac,windows etc )provide different system calls ,the executable which is generated by compiling on the same kernel only can execute on that machine.
What makes java platform independent :
JVM makes java platform independent. Java is both compiled and interpreted language .Refer to the working of execution engine of jvm for more details. Interpreter is a part of the JVM which reads the bytecode present in .class file and executes it .
Note : Java is platform independent JVM is not.
JVM for windows ,linux and mac is different altogether. The JVM of linux knows which system calls have to be made to the linux OS to run the .class file .
That is how same .class file can be executed in any operating system which has JRE installed.
Feel free to ask about any doubts .
Meaning of platform independent:-
A language is platform independent if it can be compiled on a machine and that compiled code can be executed on any computer irrespective of the underlying operating system present in that computer.
Some Examples:-
Java is platform independent because when we compile a .java file using javac a .class file is generated. This .class file can be exported to any computer and it will run as expected (note : you need not to export the .java file) .
C is not platform independent because when we compile C it generates a .exe file which will run on the computer on which it was compiled, but if you export that file to another computer having a different operating system that .exe file won't work, i.e., if you compile a C program in windows it won't work in linux or mac based operating systems.
Why C can't be platform independent:-
When we compile a C code the resultant .exe file communicates directly with the Operating system using system calls to get resources in the machine and to execute instructions. Since different kernels (linux,mac,windows etc )provide different system calls ,the executable which is generated by compiling on the same kernel only can execute on that machine.
What makes java platform independent :
JVM makes java platform independent. Java is both compiled and interpreted language .Refer to the working of execution engine of jvm for more details. Interpreter is a part of the JVM which reads the bytecode present in .class file and executes it .
Note : Java is platform independent JVM is not.
JVM for windows ,linux and mac is different altogether. The JVM of linux knows which system calls have to be made to the linux OS to run the .class file .
That is how same .class file can be executed in any operating system which has JRE installed.
Feel free to ask about any doubts .
Comments
Post a Comment