Why java is platform independent and what is JVM in java? Explain in simple way.


Platform independent and JVM in java:






    ""Java is platform independent, and that's one of the advantages of Java, Java is platform independent because of the JVM." We have heard this a lot, but what exactly does "platform" mean, what is "platform independent," and what is JVM? let's we understand this in an easy way.

Platform:


Definition : Platform is combination of Operating System and processor. 
    It is like in a computer, Processor is combination of software (Operating System) and hardware ( Processor) .
    
Just like shown in following picture



For Example : 
1)  Window Operating System + Intel Processor
2)  MAC Operating System + IBM Power PC Processer 


platform independent : 

Definition : Writing code on one platform and executing that same code on another platform without making any changes to the code.    
   To understand what platform independence is completely and easily, let's take an example:
We wrote a piece of code and named it "first.java" on one platform. and now we run this same code, i.e., "first.java" on another platform without doing any changes to the code (first.java). We call this as platform independence.
    In the Java programming language, we can run our code on any platform without making any changes. That's why we call Java is platform independent. But programming languages like C and C++ are not platform independent. Java achieved this platform intendency with the help of JVM.

JVM (JAVA VIRTUAL MACHINE) :-

Definition : JVM is a virtual machine that runs Java program and provides a runtime environment for that code.
 
    Above we wrote definition of JVM now let's understand it properly with an example,
 Let's we consider that we have 3 java files as,
1) One.java
2) Two.java
3) Three.java



first we have to compile this java files, with the help of Javac compiler,





So, as you can see in the above image, we have compiled our Java files. After compilation this .java files convert into .class files, we call this .class files as "class files" or "Byte code". and this class file / byte code is platform independent. 
    one.java convert into One.class, Two.java convert into Two.class, Three.java convert into Three.class after compilation.

1) One.java  =>  One.class 
2) Two.java  =>  Two.class
3) Three.java  =>  Three.class

After getting byte code, we need to convert this byte code into machine understandable code that is 0's (Zero's) and 1's (One's). So that computer understand our code and run it. But the problem is that our operating system cannot convert byte code into machine code on its own, we need an interpreter for this. 
    A platform, as we've seen, is a combination of an operating system and a processor.
Every operating system has a different kernel.
Example : The Windows operating system has a different kernel,
The Linux operating system has a different kernel,
The Mac operating system has a different kernel.
 



Due to the different kernels in different operating systems, we need to write different codes for different platforms, which will be a lot of work, so to minimize this problem, we have JVM. We will install JVM in each operating system, which will convert our byte code into machine code according to the kernel for each operating system. i.e., JVM is different for different operating systems, so JVM is platform dependent, and when we compile our Java file, it converts to byte code, which is common and consistent for all operating systems, which is why we say byte code is platform independent. With the help of the JVM, we can run our byte code on any platform. To understand it better, see the below image.



With the help of JVM, we interpret our byte code, i.e., we convert our byte code to machine code. then our computer run the code.


Why do we call the JVM a virtual machine?

virtual machine :  

We can only install one operating system on a single computer, but we can use a virtual machine to run two operating systems simultaneously. In our main operating system, we can install another operating system and use it with the help of a virtual machine.
Example : Installing the Linux operating system on the Windows operating system with the help of a virtual machine.
   JVM acts as an operating system, which we install in our main operating system with the help of a virtual machine and use to convert our Byte code in any operating system and run our code.
 

basic notes for educational purpose :

*Platform
1) Platform is combination of operating system and processor.

*Byte code:
7) Byte code is set of instructions for JVM.
2) Byte code is platform independent.

*Javac compiler: 
3) Javac compiler is used to compile the java code.
4) After compilation .java files are converted into .class files.

*JVM:
6) JVM is platform dependent.

*virtual machine:
We can install an operating system inside another operating system using a virtual machine.



IF you want to know complete architecture of  JVM in simple words then please comment,

Thank you...!

Comments