Why is Java Platform Independent
Java is a class-based, object-oriented programming language that is designed to have as few implementation dependencies as possible. It is intended to let application developers write once and run anywhere (WORA), meaning that compiled Java code can run on all platforms that support Java without recompilation. Java was first released in 1995 and is widely used for developing applications for desktop, web, and mobile devices. Java is known for its simplicity, robustness, and security features, making it a popular choice for enterprise-level applications.
One of the most prominent features of Java is platform independent. Platform-independent means the Java code that has been compiled generates compiled code or the byte code, and this byte code can run on all of the operating systems provided they have JVM installed in it. Basically, a program is a set of instructions that have been written in a human-readable language. Note that programs written by humans are not understood by machines. Here compilers come into the picture. A compiler compiles the program or source code into a format that can be easily understood by machines.
Therefore, one can say that a compiler is a translator that does the translation of code from human-readable format to executable code. The executable code may be run directly by the machine or maybe an intermediate representation that is run on the virtual machine. In Java, that intermediate representation is known as the Java Byte Code.
Step By Step Code Execution in Java
In C or C++ compiler compiles the code to generate the .exe file that has been executed by the machine on which the compiler is installed. However, in the case of Java, instead of .exe file, a .class file or Byte code is generated.
Java is Platform Independent but not JVM
It is important to note that JVM is platform dependent. So, for the Windows operating system, we have the JVM specific to Windows. For the Macintosh operating system, we have the Macintosh-specific operating system, and the same concept is applied to the Linux operating system too. The same is evident when we go to the websites to download the JDK (JVM is part of JDK). A list of operating system-specific JDKs is shown, and the user has to decide which one is suitable for accomplishing the task. Observe the following screenshots.
By looking at these screenshots, it is evident that JVM is not platform specific.
Important Points To Remember
Note: Now, we know that Java is platform-independent, but JVM is platform dependent. Because JVM is platform-dependent, Java is not considered as a complete platform-independent language.