How Java Achieves Platform Independence?
Story of how Java becomes write once run anywhere programming language
Hello guys, In the past, I have shared Java Multithreading questions, Java Serialization questions, System Design interview questions and Enum Interview Questions and In this post, I will talk answer one of the very popular core Java interview question.
How does Java achieve platform independence?
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.
By the way, if you are preparing for Java interviews, you can also checkout my books, Grokking the Java Interview and Grokking the Spring Boot, where I have shared frequently asked Java interview questions. You can also use code friends50 to get 50% discount for next few days.
Java Compilation and Execution Flow
For those who don't know Java is both the compiled and interpreter language.
Keep reading with a 7-day free trial
Subscribe to Javarevisited Newsletter to keep reading this post and get 7 days of free access to the full post archives.