Java is a platform-independent language
How?
Java a programming language provides platform independence, what does it mean? It
means the same Java program can be run on any platform or operating system like Windows, Linux or Solaris without any change. This is a
great benefit for someone coming from a platform-dependent programming language
like C or C++ whose code needs to be ported for every single platform because
they use native libraries, which differ in every platform. Now the question comes
how Java achieves platform independence, what makes Java programs running on
every platform without any change?
This is one of the most basic questions Java
programmers ask when they start learning the Java programming language. If you read further
you will come to know about class files, bytecode, and Java virtual machine which together
provide platform independence to Java.
One of the simplest analogies I can associate with platform independence is the person taking the red carpet with him and instead of walking on the floor, he always walks on the red carpet, no matter where he is walking.
That red carpet is the JVM, your Java program runs on JVM rather than on any particular platform or machine and that's why its platform independent.
Java Compilation and execution
For those
who don't know Java is both the compiler and interpreter language. When you compile
a Java program creates a .class file which is a collection of byte code, these
byte codes are not machine instructions instead they are instructions which Java a virtual machine can understand.
Since every Java program runs on Java virtual
machine, the same byte code can be run on any platform. the key is byte code is not
machine instruction they are platform-independent instruction to JVM.
On another hand, JVM or Java virtual machine is platform-dependent because it
converts byte code into machine level instruction which is platform-specific
and that's why you have a different version of JDK and JRE for windows and Linux
because both JDK and JRE come with Java virtual machines. if you are confused
between JVM, JRE and JDK then read my post on the difference between JDK, JRE, and JVM in Java.
Byte code is created when you compile Java program using Java compiler "javac" and byte code runs on JVM which is created by running the "java" command. In detail when you run "java" command it creates Java virtual machine, loads Main class specified in the command line and calls the standard main method in java.
In summary
combination of bytecode and JVM makes the Java program platform-independent. Write
once run everywhere was Java’s mantra when it started ruling the programming world
in the mid and late ’90s.
Always remember, Java programs are platform independent but JVM is not. That's why you have different JVM and JRE installations for different platforms like Mac, Windows, Linux, or Solaris. Similarly, there are different JVM for 32-bit and 64-bit machines.
Java has always been famous for its platform independent and write once run everywhere (WORA) feature and that's why it is still popular after 25 years of its creation. There are many more programming language and platform created after Java which uses same feature like .NET CLR (Common Language Runtime) which also offer platform independency for .NET application.
Life Cycle of JVM :
ReplyDeleteWhen a Java application starts, a runtime instance is born. When the application completes, the instance dies. If you start three Java applications at the same time, on the same computer, using the same concrete implementation, you'll get three Java virtual machine instances. Each Java application runs inside its own Java virtual machine.
For Example :
If we have a Node manager which contains 3 Managed server having instance of JVM, then there will be 3 instance of JVM running on managed server, all of them will be pointing to the Node manager.
The source code is first written in plain text files ending with the .java extension. Those source files are then compiled into .class files by the javac compiler. A .class file does not contain code that is native to your processor; it instead contains bytecodes — the machine language of the Java Virtual Machine (Java VM). The java launcher tool then runs your application with an instance of the Java Virtual Machine.
Please chek the below UrL for the details :
http://javatopics77.blogspot.in/p/java-virtual-machine-jvm-introduction.html
This explains what is JVM and how does it work.Very useful info.
how to install JVM
ReplyDeleteAfter finding out how virtual machines enable platform independece can anyone help
ReplyDeletethis explanation perfectly cleared my confusion on how java is platform independent. Thank you.
ReplyDeletethank you so much.... so very useful
ReplyDeleteYes, exactly
ReplyDeleteIs JDK JRE JVM platform independent or dependent ?
ReplyDeleteHello @Unknown, they are platform dependent, that's why you have separate JDK installer package for Mac, Windows, and Linux. They are coded in C/C++, JAR files are platform independent, they are Java binary code.
Deletegreat explanation thanks
ReplyDeleteglad you liked it, thx for kind words
DeleteSo in other words Java is inturn platform dependent , not the java programs but they require JVM which is paltform dependent so at last we can say it seems fully independent but its actaully not .
ReplyDeleteIt have benifits than c/c++ beacuse in java we don't need to compile a program again n again because bytecode is generated once for all the JVM installed devices .
Means it is bytecode that plays important role in making Java Platform independent...
Please correct me or add your thoughts also ....