What is a Java Virtual Machine?

By Barry Burd

Luckily, you never have to write or decipher Java bytecode. Writing bytecode is the compiler’s job. Deciphering bytecode is the Java Virtual Machine’s job.

Generally, computers don’t execute Java bytecode instructions. Instead, each kind of computer processor has its own set of executable instructions, and each computer operating system uses the processor’s instructions in a slightly different way.

“A Java Virtual Machine runs your code (and other peoples’ Java code) on your computer.”

— Barry Burd, Java For Dummies, 6th Edition

Example Java listings

Here’s a hypothetical situation: Imagine that you run the Linux operating system on a computer that has an old Pentium processor. Your friend runs Linux on a computer with a different kind of processor — a PowerPC processor. (In the 1990s, Intel Corporation made Pentium processors, and IBM made PowerPC processors.)

This listing contains a set of instructions to display Hello world! on the computer screen. The instructions work on a Pentium processor running the Linux operating system.

.data
msg:
        .ascii  "Hello, world!n"
        len = . - msg
.text
    .global _start
_start:
        movl    $len,%edx
        movl    $msg,%ecx
        movl    $1,%ebx
        movl    $4,%eax
        int     $0x80
        movl    $0,%ebx
        movl    $1,%eax
        int     $0x80

Here is another set of instructions to display Hello world! on the screen. The instructions in this listing work on a PowerPC processor running Linux.

.data
msg:
        .string "Hello, world!n"
        len = . - msg
.text
        .global _start
_start:
        li      0,4
        li      3,1
        lis     4,msg@ha
        addi    4,4,msg@l
        li      5,len
        sc
        li      0,1
        li      3,1
        sc

The instructions here run smoothly on a Pentium processor. But these instructions mean nothing to a PowerPC processor. Likewise, other instructions run nicely on a PowerPC, but these same instructions are complete gibberish to a computer with a Pentium processor. So your friend’s PowerPC software might not be available on your computer. And your Intel computer’s software might not run at all on your friend’s computer.

Now go to your cousin’s house. Your cousin’s computer has a Pentium processor (just like yours), but your cousin’s computer runs Windows instead of Linux. What does your cousin’s computer do when you feed it the Pentium code in Listing 2-3? It screams, “Not a valid Win32 application” or “Windows can’t open this file.” What a mess!

What is Java bytecode?

Java bytecode creates order from all this chaos. Java bytecode is something like the code in Listings 2-3 and 2-4, but Java bytecode isn’t specific to one kind of processor or to one operating system. Instead, a set of Java bytecode instructions runs on any computer.

If you write a Java program and compile that Java program into bytecode, then your computer can run the bytecode, your friend’s computer can run the bytecode, your grandmother’s supercomputer can run the bytecode, and with any luck, your cellphone or tablet can run the bytecode.

With Java, you can take a bytecode file that you created with a Windows computer, copy the bytecode to who-knows-what kind of computer, and then run the bytecode with no trouble at all. That’s one of the many reasons why Java has become popular so quickly. This outstanding feature, which gives you the ability to run code on many different kinds of computers, is called portability.

What does the Java Virtual Machine do?

What makes Java bytecode so versatile? This fantastic universality enjoyed by Java bytecode programs comes from the Java Virtual Machine. The Java Virtual Machine is one of those three tools that you must have on your computer.

Imagine that you’re the Windows representative to the United Nations Security Council. (See the figure.) The Macintosh representative is seated to your right, and the Linux representative is on your left. The distinguished representative from Java is at the podium. The Java representative is speaking in bytecode, and neither you nor your fellow ambassadors (Mac and Linux) understand a word of Java bytecode.

But each of you has an interpreter. Your interpreter translates from bytecode to Windows while the Java representative speaks. Another interpreter translates from bytecode to Macintosh-ese. And a third interpreter translates bytecode into Linux-speak.

Think of your interpreter as a virtual ambassador. The interpreter doesn’t really represent your country, but the interpreter performs one of the important tasks that a real ambassador performs.

The interpreter listens to bytecode on your behalf. The interpreter does what you would do if your native language were Java bytecode. The interpreter pretends to be the Windows ambassador, and sits through the boring bytecode speech, taking in every word, and processing each word in some way or other.

You have an interpreter — a virtual ambassador. In the same way, a Windows computer runs its own bytecode-interpreting software. That software is the Java Virtual Machine.

An imaginary meeting of the UN Security Council.

An imaginary meeting of the UN Security Council.

A Java Virtual Machine is a proxy, an errand boy, a go-between. The JVM serves as an interpreter between Java’s run-anywhere bytecode and your computer’s own system. While it runs, the JVM walks your computer through the execution of bytecode instructions.

The JVM examines your bytecode, bit by bit, and carries out the instructions described in the bytecode. The JVM interprets bytecode for your Windows system, your Mac, or your Linux box, or for whatever kind of computer you’re using. That’s a good thing. It’s what makes Java programs more portable than programs in any other language.