Skip to main content

JVM

Java Virtual Machine

Pronunciation
jay-vee-EM
Updated 2 min read

Share this page

Send the link, quote the definition with a link back, or show it as a card on your own site.

https://softwaredictionary.org/terms/jvm

In short

The JVM runs Java bytecode, so the same compiled program works on any operating system; Kotlin, Scala and Clojure compile to that bytecode too.

What is the JVM?

Java source code isn't compiled for a particular processor. The javac compiler turns it into bytecode, stored in .class files and packaged in JAR files, and the JVM runs that bytecode on whatever machine it is installed on. That is the idea behind Java's old slogan, write once, run anywhere: the program is the same on Windows, macOS and Linux, and only the JVM differs.

The JVM does much more than read bytecode. It loads classes as they are needed, checks the bytecode for safety, manages memory with a garbage collector, and compiles the most frequently run code into native machine code with a just-in-time (JIT) compiler, which is why long-running Java servers get faster after they warm up. HotSpot, the most widely used implementation, comes with OpenJDK, the open-source project behind most Java distributions.

Because the JVM runs bytecode rather than Java itself, other languages target it too: Kotlin, Scala, Clojure and Groovy compile to the same bytecode and can use any Java library. To develop, you install a JDK (Java Development Kit), which includes the compiler and the JVM. The JVM's specification is public, so several vendors build their own distributions of it.

Key takeaways

  • The JVM runs Java bytecode, so a compiled program works on any operating system.
  • It manages memory with a garbage collector and speeds up hot code with a JIT compiler.
  • Kotlin, Scala, Clojure and Groovy also compile to JVM bytecode.
  • A JDK contains the compiler and the JVM; HotSpot, from OpenJDK, is the usual JVM.

Example

From source code to bytecode to the JVMbash
# Hello.java: public class Hello { public static void main(String[] a) { System.out.println("Hi"); } }
javac Hello.java   # compile to bytecode: Hello.class
java Hello         # the JVM loads Hello.class and runs it
# Hi
javap -c Hello     # print the bytecode instructions the JVM executes

Readers ask

What is the difference between the JDK, the JRE and the JVM?

The JVM is the engine that runs bytecode. The JRE was the JVM plus the standard library, enough to run programs. The JDK adds the development tools, such as the javac compiler. Since Java 11, Oracle no longer ships a separate JRE, and you install a JDK.

Is the JVM the same kind of virtual machine as VirtualBox?

No. A system virtual machine imitates a whole computer so it can run an operating system. The JVM is a process virtual machine: an ordinary program that runs one application's bytecode.

See also

Sources

Spotted a mistake or something missing on this page?Suggest an edit

Read a random page
Open today's review
Switch to the dark theme
Read this page in Türkçe

More

Settings