JVM
Java Virtual Machine
- Pronunciation
- jay-vee-EM
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
# 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 executesReaders 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
- JavaProgramming Languages, p. 15Java is a statically typed, object-oriented programming language that compiles to bytecode for the Java Virtual Machine, so programs run on many platforms.
- KotlinProgramming Languages, p. 18Kotlin is a concise, statically typed language that runs on the JVM, works seamlessly with Java, and is widely used for Android and server-side development.
- ScalaProgramming Languages, p. 30Scala is a statically typed JVM language that combines object-oriented and functional programming and is widely used for data engineering and backend systems.
- ClojureProgramming Languages, p. 6Clojure is a modern, dynamic dialect of Lisp that runs on the JVM, emphasizing immutable data, functional programming and interactive, REPL-driven development.
- JIT CompilationProgramming Fundamentals, p. 32JIT compilation turns the busiest parts of a program into machine code while it runs, combining an interpreter's quick start with compiled speed.
- Garbage CollectionProgramming Fundamentals, p. 23Garbage collection is automatic memory management in which the language runtime finds data a program can no longer use and frees that memory for reuse.
- Virtual MachineDevOps & Cloud, p. 53A virtual machine is a software-based computer that runs its own operating system on shared physical hardware, isolated from other machines on the same host.
Sources
Spotted a mistake or something missing on this page?Suggest an edit