Understanding Java -1

Understanding Java -1

HOW IS JAVA PLATFORM INDEPENDENT?

Java is platform-independent because of byte code and JVM.

Execution of a java program

As you can see from the image above, the source code(.java) is compiled into byte code, the .class file, which is platform-independent. This byte code is converted into machine code by JVM(Java Virtual Machine).

Other Languages Platform Dependent - Why?

Here by platform, we mean operating system. Every os has its own way of executing files and handling instructions. Thus converting the source code into machine language makes the languages platform dependent. Other platform-dependent languages do not generate an intermediate code like byte code, instead, they generate object code(machine code) which is platform-specific (.exe).

Java Platform Independent - How?

This disadvantage is handled by JVM in Java. A different version of JVM is present for a different machines. Windows has its own version of JVM, Linux has its own, etc., It converts the independent byte code to the respective machine code. In other words, the JVM reads and converts the byte code to machine-dependent code.

Conclusion

Developers have no more worry about writing system-specific code. They can write a single piece of code and run it on Windows, Linux, Unix, etc. because the JVM of each OS has the full responsibility of converting the source code into machine code by executing the intermediate independent byte code(.class) which is produced after compiling the source code(.java).