Java Virtual Machine:  Introduction

Honors Computer Systems Organization (Prof. Grishman)

Why a Java Virtual Machine?

We need a way of executing Java which is
  • platform independent
  • efficient
  • compact (since programs have to be loaded over the web)
  • secure (so a program loaded over the web can't destroy your machine)

  • Source code would be platform independent, but neither efficient nor compact;  machine code would be efficient but not platform independent.  So SUN defined a virtual machine, JVM.  Java source code is translated to JVM code, which is then downloaded along with web pages.  The web browser includes a JVM implementation, which either interprets the JVM or translates it into machine code on the fly (JIT:  just-in-time compiler).  Processor chips have also been developed which execute JVM code directly.

    JVM is a stack machine;  this makes it simple to generate JVM code, and makes the JVM code compact.

    Java Resources

    The Java Development Kit (JDK), provided by SUN, includes a java compiler (javac) and a JVM implementation (java).

    Jasmin is an assembler for JVM assembly language.  The Jasmin home page contains extensive information about Jasmin and JVM.

    D-Java is a disassembler, which takes a JVM machine language program (a .class file) and translates it into Jasmin assembly language.

    (We will execute these using a DOS window;  it is easier if you put all the bin directories on the DOS PATH.)

    A simple example

    Java is an object oriented language.  We define a set of classes, and for each class define a set of methods which operate on the class.  The basic compilation unit is a Java file containing the definition of a class,  It is translated into a JVM machine language file, X.class.

    If we create a simple Java class (file with one method, which adds two integers:

    public class mymath {
      public static int sum (int a, int b) {
        return a + b;

    and then compile it (with javac, creating file mymath.class) and disassemble it (with D-java mymath), we get

    ; Output created by D-Java (

    ;Classfile version:
    ;    Major: 45
    ;    Minor: 3

    .class  public synchronized mymath
    .super  java/lang/Object

    ; >> METHOD 1 <<
    .method public <init>()V
        .limit stack 1
        .limit locals 1
    .line 1
        invokenonvirtual java/lang/Object/<init>()V
    .end method

    ; >> METHOD 2 <<
    .method public static sum(II)I           ; takes two int args and returns an int result
        .limit stack 2
        .limit locals 2
    .line 3
        iload_0                              ; load integer arguments (passed as local
        iload_1                              ;   variables 0 and 1) onto stack
        iadd                                 ; integer add, leaving result on stack
        ireturn                              ; return integer on top of stack
    .end method