Nepedia
Back to Discover
Advanced Reader
💻

Java virtual machine

technology Maturity 5-7 war conflict Next article →

This article covers sensitive topics: war_conflict. Parents can manage visibility in Parental Controls.

A Java Virtual Machine, or JVM, is an abstract computer defined by a formal specification. It acts as a layer between a computer program and the actual hardware. This allows a computer to run Java programs and other "JVM languages" that are compiled into Java bytecode. Bytecode is a special, hardware-independent binary format. Because the JVM follows a strict specification, programs can run on many different types of hardware without being rewritten. This concept is known as interoperability.

JvmSpec7
JvmSpec7

The JVM functions through a complex architecture of memory and data management. It uses a garbage-collected heap to store objects and arrays. Code, constants, and other class data are kept in a separate area called the method area. While the method area is logically part of the heap, some implementations treat it separately. Each individual thread within the JVM has its own call stack, often called a Java Virtual Machine stack. This stack stores frames, which are created whenever a method is called and destroyed when that method exits. Each frame contains an operand stack for math computations and an array for local variables.

JvmSpec7
JvmSpec7

To prepare a program for execution, the JVM uses a component called a class loader. This unit must recognize any data that follows the Java class file format. The class loader follows a strict three-step sequence: loading, linking, and initialization. During loading, it finds and imports the binary data for a specific type. Next, it performs linking, which involves verification, preparation, and resolution. Verification ensures the imported type is correct, while preparation allocates memory for class variables. Finally, initialization invokes Java code to set class variables to their proper starting values. There are typically three types of class loaders: the bootstrap class loader, the extension class loader, and the system or application class loader.

Memory management in the JVM is handled automatically through a process called garbage collection. Unlike languages like C++ or Rust, which use manual memory management, Java relies on a garbage collector to clean up. In Java, objects are stored as references in a heap. If a programmer tried to manually delete an object, it could result in "dangling pointers," where a reference points to empty space. To prevent this, the JVM manages the heap. Different versions of Java use different garbage collection algorithms. For example, older versions like Java 6u14 used Serial or Parallel collectors. Newer versions like Java 23 use the GenZGC collector by default. Other available collectors include G1, ZGC, Shenandoah, and the experimental Epsilon.

JvmSpec7
JvmSpec7

The history of the JVM is documented in a set of rules known as the "blue book." This is officially titled The Java Virtual Machine Specification. The first and second editions of this specification are available online. Changes to these rules are managed through the Java Community Process, such as the JSR 924 update. Oracle owns the Java trademark and provides commercially supported releases based on the OpenJDK runtime. The OpenJDK project provides the reference implementation of the JVM as open source code. This implementation includes a Just-In-Time (JIT) compiler called HotSpot, which is the most commonly used JVM in the world.

JvmSpec7
JvmSpec7

Security and stability are core philosophies of the JVM design. The JVM includes a bytecode verifier to ensure that programs are inherently safe. This tool prevents user programs from crashing the host machine or interfering with other operations. It also blocks common programmer errors, such as trying to access the end of an array or using uninitialized pointers. This protection is achieved through the combination of the class model, the garbage-collected heap, and the verifier. By checking the code before it runs, the JVM protects trusted data structures from being corrupted by untrusted code.

Finally, the JVM supports a wide variety of programming languages. A JVM language is any language whose functions can be expressed in a valid class file. Some well-known examples are JRuby and Jython, which are ports of the Ruby and Python languages. Newer languages built specifically for the JVM include Clojure, Groovy, Scala, and Kotlin. These languages benefit from interoperability, meaning a Scala library can work seamlessly with a Java program. This ecosystem allows developers to choose the best tools for their specific needs while staying within the safe, high-performance environment of the virtual machine.

712 words

Where will your curiosity go next?

Keep exploring at your chosen reading level.

🖼️ Images & Media (1)
File:JvmSpec7.png
JvmSpec7.png
More to explore

What is Nepedia?

A free, ad-free encyclopedia for children. Every article is written at five reading levels, so the same page works for a five-year-old and a fifteen-year-old — use the level switcher above to see this one change. No account needed to read.