Week 8

Exercises on virtual machines

  • Translate the following class into Java Virtual Machine bytecode by hand:
    public class Cons {
      protected Object first;
      protected Cons rest;
     
      public Cons(Object f, Cons r) 
      { super();
        first = f; rest = r;
      }
     
      public void setFirst(Object newfirst) 
      { first = newfirst; }
     
      public Object getFirst() 
      { return first; }
     
      public Cons getRest()
      { return rest; }
     
      public boolean member(Object item)
      { if (first.equals(item))
           return true;
        else if (rest==null)
           return false;
        else
           return rest.member(item);
      }
    }
    
    Can you beat joosa+ in terms of bytes required for all methods in the class? What about javac? You probably want to refer to the JVM specification.

Maintained by Christopher J. F. Pickett. [HOME]