The Java Virtual Machine (JVM) is written in the C++ programming language. The original JVM was developed by Sun Microsystems, which was later acquired by Oracle, using C++ due to some key advantages that language provided.
C++ enabled tight control over memory management and access to low-level machine operations which were critical for delivering good performance. The portability of C++ also made it easier to support JVM implementations across operating systems and hardware architectures. Additionally, leveraging C++ meant they could reuse some code across other products at Sun.
Why C++ was chosen for writing the JVM
There were a few key reasons why the creators of Java, James Gosling and others at Sun Microsystems, chose to use C++ to implement the original Java Virtual Machine:
- Performance: C++ gives a high degree of control over low-level operations and memory management which enabled greater performance tuning and efficiency versus higher-level languages.
- Portability: C++ code can be compiled to target different hardware platforms and operating systems, making it easier to write a cross-platform JVM implementation that works across OSes and chips.
- Existing code reuse: Sun already had a lot of C and C++ code for existing products that they could reuse and integrate with when writing the JVM. This sped up development.
- Superior tools and debugging support: C++ had more mature compiler and debugging technology at the time Java was created versus alternatives like Java itself. This aided JVM development.
By leveraging the strengths of C++ for the inaugural JVM, the key goals of efficiency, portability, rapid iteration, and production-grade robustness were able to be achieved by Sun’s engineers building the platform.
JVM architecture and components written in C++
The core components of the Java Virtual Machine architecture, from the classloaders to the runtime, were all originally written in C++. This includes key elements like:
- Classloader subsystems: The classloaders that locate, load, and initialize Java classes into the JVM runtime are C++ code.
- Runtime engine: The core runtime component that handles thread scheduling, garbage collection, and JIT (Just-In-Time) compilation was written in C++.
- Memory manager: The JVM has a built-in automatic memory manager that allocates and frees up objects and variables. This subsystem leverages C++ for the original implementation.
- JIT compilers: The Just-In-Time (JIT) compilers that dynamically translate Java bytecodes into optimized machine code are written in C++.
- Native method interface: The native method interface allows Java code to interoperate with applications and libraries written in other languages like C/C++. Implemented in C++.
Using C++ for implementing the key subcomponents enabled tight control and tuning of these layers to improve efficiency, throughput, scalability, and runtime performance of Java applications, while also giving the JVM cross OS/platform portability.
C++ evolution to support leading JVMs
The C++ language itself has evolved significantly since the first JVM introduction over 2 decades ago. Modern C++ includes many enhancements around templates, lambdas, threading, memory management, and efficiency.
As a result, today’s leading JVM implementations from Oracle JDK, OpenJDK, Azul Zing, Amazon Corretto etc continue to leverage the latest C++ standards and compilers to optimize the runtime code. Continued usage of C++ ensures consistent performance gains and scalability improvements are delivered to Java apps with each new JVM release.
So while Java itself has changed dramatically across 8 major versions over 25+ years, the choice to use C++ for implementing the underlying JVM foundation still delivers big benefits today even as both languages have evolved.
Key takeaways on JVM language choice
- Sun chose C++ due to its maturity, efficiency, tight hardware access and existing code reuse opportunities
- C++ enabled performance tuning, cross-platform support and good tooling during early JVM development
- The classloaders, runtime, compilers and memory managers are key JVM components written in C++
- Modern JVMs leverage the latest C++ features to keep improving speed and scalability
In summary, the core of the pioneering Java Virtual Machine was written in C++ by Sun Microsystems to deliver greater performance, portability and reuse leveraging C++’s strengths for systems programming. C++ offered superior control over hardware resources and memory management versus other popular languages of the era like C or Java itself in the mid-90’s. This choice enabled Sun’s engineers to optimize the JVM for efficiency and runtime speed since the beginning.
Today’s leading JVM’s continue benefiting from the ongoing evolution of C++ with better compilers, templates and language capabilities. By using modern C++, JDK vendors like Oracle and OpenJDK can keep improving Java’s performance and scalability with each new release. So C++ remains as relevant today for advancing Java’s capabilities as it was when powering the original JVM over 25 years back!
Frequently Asked Questions
Q: Why was C chosen over C++ originally to build the JVM?
A: C++ was chosen over C for implementing the original JVM to leverage C++’s OOP features like classes/objects, inheritance, templates etc. C++ also offered better memory management, performance tuning and tooling support.
Q: What core components of the JVM are written in C++?
A: Key components like the classloaders, JIT compilers, garbage collectors, runtime engine, native interfaces and memory managers are written in C++.
Q: Is the JVM implemented purely in C++ or does it use other languages?
A: While most JVM cores use C++, some providers mix in other languages too e.g. LLVM compilers written in C++ assemble Java bytecode on the fly into machine code.
Q: Has Java replaced C++ in building modern JVMs like OpenJDK?
A: No, most leading JDKs from Oracle, OpenJDK etc still rely on C++ for performance critical components. Both languages evolve to improve JVM capabilities.
Q: Why has C++ remained the primary JVM implementation language over decades?
A: C++ gives control over hardware resources critical for performance, portability across OS/chips, allows leveraging latest compiler improvements and enables reuse of libraries/code.
Q: What recent C++ advancements help improve current JVMs vs original?
A: Modern C++ has better templates, lambdas, threading, memory management and efficiency. These help enhance today’s JVM speed, scaling and capabilities.
Q: Does using C++ limit which operating systems the JVM can support?
A: No, C++ can target many chip architectures and OSes. JVM in C++ runs on popular platforms like Windows, Linux, macOS, Solaris etc enabling Java’s “write once, run anywhere” value prop.
Q: Can the JVM be compiled to machine code completely instead of relying on C++?
A: Some ahead-of-time compilers can compile Java to machine code without a JVM. But code portability issues arise without a virtual machine, hindering Java’s cross-platform abilities.
Q: What are the pros and cons of using C vs C++ to write a modern JVM?
A: C++ is better performance, OOP support and tools. But C offers closer hardware control and simplicity. Most JVMs leverage C++ but may use C code in select hotspots for latency/throughput.
Q: Why not rewrite the JVM completely in Java for better integration?
A: Java cannot match C++’s system programming strengths for low-level efficiency. This would affect JVM startup, memory usage, thread scheduling if rewritten fully in Java.
Q: How has GraalVM and similar projects changed JVM language usage?
A: Graal and others allow adding additional languages for the JVM besides Java. This expands capabilities but still relies on an underlying C++ foundation.
Q: Do alternative JVM languages like Scala or Clojure replace its C++ internals?
A: No, they run atop the core C++ JVM just like Java. So C++ Underpinnings for classloading, JIT, etc enable these languages to work cross-platform.
Q: Why can’t I find the C++ source code for OpenJDK/Oracle JVMs publicly?
A: Some core C++ code is not fully published as open source. But many classes/libraries are public, and others get revealed over time as code bases shift licensing.
Q: Does Android use the standard C++ based JVM or something different?
A: Android uses its own runtime- Dalvik/ART built for constrained mobile device resources. But these still rely on C/C++ internals similar to desktop JVMs.
Q: Are there efforts to port the JVM C++ internals to other languages like Rust?
A: Some research exists on using Rust or Zing to eventually replace C++ in JVMs for better security and safety. But so far, C++ leads in maturity and tuning for Java workloads.