Week 8

Virtual Machines


    1. Try to cheat the Java bytecode verifier in the following manners. For each case, report which error message the bytecode verifier gives you. (If it gives none, then you really managed to cheat it :P)
      1. perform address arithmetic Fake1.j
      2. pop something off an empty stack Fake2.j
      3. construct two different types of stack (of the same height) in the branches of an if-else statement Fake3.j
    2. Consider the following program. Which number does it output? Answer: 3 Test.java Inspect the generated bytecode, e.g. using javap -c Test.
      class A {
      int m(Object x, Object y) {
      return 1;
      }
      }
      class B extends A {
      int m(Object x, String y) {
      return 2;
      }
      }
      class C extends B {
      int m(Object x, Object y) {
      return 3;
      }
      }
      public class Test {
      public static void main(String[] args) {
      A a = new C();
      System.out.println(a.m(null,"abc"));
      }
      }
    3. Consider now the same code but where A has been extended:
      class A {
      int m(Object x, Object y) {
      return 1;
      }
      int m(Object x, String y) {
      return 4;
      }
      }
      Which number does Test output now? Answer: 2 Test2.java Discuss the exact semantics of the invokevirtual instruction.
    4. Slide 21 on type checking seems to show nothing with respect to method overloading. Why is that? Answer: This slide was with respect to JOOS, which allows no overloading for methods.

Maintained by Eric Bodden. [HOME]