So, why is Java a platform-independent language? You can break this question in two:
- Why did Sun Microsystems make Java platform-independent?
- How is Java's platform independence achieved?
Java's cross-platform origins
First, why is Java cross-platform? Let's dig into the history of the language first.
Sun Microsystems, the inventor of Java, was a popular vendor in the computer workstation market in the 1990s. As Sun grew, it acquired a number of competitors, including Trancept Systems, Praxsys Technologies and Silicon Graphics Cray Business Systems Division. Sun itself was acquired by Oracle in 2009, but that's another story. With the constant acquisition of disparate computer architectures, Sun found itself supporting a variety of programming languages, that worked differently on different operating systems.
To address this unenviable situation, Sun invested in the development of a single programming language that would work on a variety of OSes and computer architectures. This became known as the Green Project. From the Green Project came the Oak development API, which then evolved into the platform-independent Java programming language.
The direction the Java programming language took as the Green Project grew beyond platform independence and into visions of interconnected TV set-top boxes, and eventually into cross-platform support for networked computers linked through the world-wide web.
But regardless of how the Green Project evolved, it was the problems associated with the management of their own disparate, internal architectures that motivated Sun to create the platform-independent Java programming language.
How is Java's platform independence achieved?
Most programming languages, such as C++ or Visual Basic, compile directly into executables or binaries that will run only on a specific OS or hardware architecture. That's why so many programs that run on a Mac are not compatible on a PC.
Java source code does not compile directly into native machine code. Instead, Java compilation generates bytecode, which contains a high-level set of machine instructions that are general enough not to be platform-specific. To run this bytecode, a computer must have a Java Virtual Machine (JVM) installed. When a Java program runs, the JVM interprets the bytecode and then generates native machine code that can be consumed by the CPU.
It's worth noting that the JVM itself is not a cross-platform technology.
Every architecture and OS must have a specifically designed and built JVM in order for platform-independent Java programs to run. However, since the need for a JVM is universally seen as a priority, every modern OS provides one. This is why the Java's promotional tagline has always been: "Write once, run anywhere."
Dig Deeper on JSRs and APIs
Related Q&A from Cameron McKenzie
What is the difference between a variable written in kebab case vs. camel case? Here we'll show you how these naming conventions differ and when to ... Continue Reading
The use of white space is what separates camel case and snake case. Camel case has none, while snake case uses underscores. Let's review some ... Continue Reading
Learn the difference between compiled languages like Go and interpreted ones like Java. One language is platform agnostic. The other can run a bit ... Continue Reading