Is Java platform independent? Why and How?

Java is a platform independent programming language that means it is architecture neutral. In a platform independent language the same program can run on multiple platforms, for example, Macintosh, Windows, Linux, and others. Now the real question is what makes Java a platform independent programming language?

Before answering this question first let us understand, why C/C++ is platform dependent? When we write program in C/C++ and compile it, it is directly compiled into an executable form known as binary code or machine code. This machine code is understood only by the platform it has been compiled upon. For example, when you compile a C/C++ program on Windows OS, the executable machine code is generated for that program is specific to only Windows OS and cannot be made to run on other platforms like LINUX, Solaris and Macintosh.

On the contrary, Java source code is not compiled for a particular platform, but rather into a special intermediate format known as bytecode, which is said to be both platform independent and architecture neutral. That is, no matter whether a Java program is compiled under Sun Solaris, Windows, Linux, or any other operating system for which a Java compiler is available, the resultant bytecode turns out to be the same and hence can be run on any computer for which a (platform-specific) Java Virtual Machine (JVM) is available.

Therefore, in order to achieve platform independence, Java creators made Java program execution a two-step process. In first step Java program is compiled and an intermediate code format known as bytecode is generated. In second step, this intermediate code is interpreted and output is produced.

If you have ever written and executed a Java program then you must have noticed that you first compile the Java program by using javac command. If the program is error free then it is compiled successfully and bytecode file is generated with .class extension. In the second step, you execute this .class file with the help of java command.

Here bytecode is platform independent, no matter on which platform you compile your .java file, generated bytecode will remain identical then a platform DEPENDENT Java Virtual Machine (JVM) runs directly on the underlying platform and executes the bytecode. So here, byte code is platform independent and JVM is platform specific.

From our understanding until this point we can draw the following conclusions.

  • JAVA compiler (javac) compiles the JAVA files.
  • The JAVA compiler generates .class file. It is also known as bytecode. It is not the machine native code (unlike C compiler).
  • The bytecode generated is a non-executable code and needs an interpreter to execute on a machine. This interpreter is the JVM and the Bytecode is executed by the JVM.
  • So, in case of Java, it is the magic of Bytecode that makes it platform independent.
  • Please note that while JAVA is platform-independent language, the JVM is platform-dependent. Different JVM is designed for different OS and byte code is able to run on different OS.

Hope you have enjoyed reading about platform independent Java. Please do write us if you have any suggestion/comment or come across any error on this page. Thanks for reading!



Share this page on WhatsApp

Get Free Tutorials by Email

About the Author

is the founder and main contributor for cs-fundamentals.com. He is a software professional (post graduated from BITS-Pilani) and loves writing technical articles on programming and data structures.