Download The Java Virtual Machine - Quiz 3 | CS 2110 and more Quizzes Computer Science in PDF only on Docsity!
Under the Hood: The Java Virtual Machine, Part II CS2110 Fall 2008 Lecture 24
Announcements
• A5 due date postponed!
• New due date Monday Dec 8, 11:59pm
CS2110 F08 Quiz 3
Answer briefly the following questions.
1. Explain the difference between shadowing and overriding.
Shadowing occurs when a field of a class has the same name and type as a field
of a superclass, or when a local variable of a class has the same name and type
as a field of that class. It is useful only in limited circumstances (e.g.
initializing a field with a parameter). Overriding occurs when a method of a
class has the same name and signature as a method of a superclass, and is one
of the most useful features of object oriented languages.
2. Explain the difference between interfaces and abstract classes.
Interfaces may contain only constants and abstract methods (method signatures
but no implementations). Abstract classes may contain fields and both abstract
and concrete methods. Neither interfaces nor abstract classes can be directly
instantiated with new.
3. Explain the difference between static and dynamic types.
Expressions have static types. The type of any expression is known to the Java
compiler at compile time, before the program is run. Objects have dynamic
types. Dynamic types are only known at runtime. An object receives its
dynamic type when it is created with new.
Java program Java compiler Java bytecode (.class files) Compile for platform with JIT Interpret with JVM run native today last time
Instance Method Dispatch
x.foo(...)
• compiles to^ invokevirtual
• Every loaded class knows its superclass
– name of superclass is in the constant pool
– like a parent pointer in the class hierarchy
• bytecode evaluates arguments of
x.foo(...), pushes them on the stack
• Object^ x^ is always the first argument
Instance Method Dispatch
invokevirtual foo (...)V
• Name and type of^ foo(...)^ are arguments to
invokevirtual (indices into constant pool)
• JVM retrieves them from constant pool
• Gets the dynamic (runtime) type of^ x
• Follows parent pointers until finds^ foo(...)V
in one of those classes – gets bytecode from
code attribute
local variable array
operand stack
this p 0 p 1 p 2
parameters other locals
maxLocals
maxStack
= reference type
= integer (boolean, byte, ...)
= continuation
String
Hash-
table
Object
String-
Buffer
User-
Class
int[ ]
= useless
Stack Frame of a Method
Instance Method Dispatch
byte[] data; void getData() { String x = "Hello world"; data = x.getBytes(); } Code(maxStack = 2, maxLocals = 2, codeLength = 12) 0: ldc "Hello world" 2: astore_ 3: aload_0 //object of which getData is a method 4: aload_ 5: invokevirtual java.lang.String.getBytes ()[B 8: putfield A.data [B 11: return
Exception Handling
• Finds an exception handler^ →^ empties
stack, pushes exception object, executes
handler
• No handler^ →^ pops runtime stack, returns
exceptionally to calling routine
• finally clause is always executed, no
matter what
startRange start of range handler is in effect
endRange end of range handler is in effect
handlerEntry entry point of exception handler
catchType exception handled
Exception Table Entry
• startRange → endRange give interval of instructions
in which handler is in effect
• catchType is any subclass of Throwable (which is a
superclass of Exception) -- any subclass of
catchType can be handled by this handler
0: aconst_null 1: astore_ 2: new java.lang.Object 5: dup 6: invokespecial java.lang.Object. ()V 9: astore_ 10: aload_ 11: checkcast java.lang.Integer 14: astore_ 15: getstatic java.lang.System.out Ljava/io/PrintStream; 18: aload_ 19: invokevirtual java.lang.Integer.intValue ()I 22: invokevirtual java.io.PrintStream.println (I)V 25: getstatic java.lang.System.out Ljava/io/PrintStream; 28: ldc "finally!" 30: invokevirtual java.io.PrintStream.println (Ljava/lang/String;)V 33: goto # 36: astore_ 37: getstatic java.lang.System.out Ljava/io/PrintStream; 40: ldc "y was not an Integer" 42: invokevirtual java.io.PrintStream.println (Ljava/lang/String;)V 45: getstatic java.lang.System.out Ljava/io/PrintStream; 48: ldc "finally!" 50: invokevirtual java.io.PrintStream.println (Ljava/lang/String;)V 53: goto # 56: astore_ 57: getstatic java.lang.System.out Ljava/io/PrintStream; 60: ldc "y was null" 62: invokevirtual java.io.PrintStream.println (Ljava/lang/String;)V 65: getstatic java.lang.System.out Ljava/io/PrintStream; 68: ldc "finally!" 70: invokevirtual java.io.PrintStream.println (Ljava/lang/String;)V 73: goto # 76: astore 4 78: getstatic java.lang.System.out Ljava/io/PrintStream; 81: ldc "finally!" 83: invokevirtual java.io.PrintStream.println (Ljava/lang/String;)V 86: aload 4 88: athrow 89: return From To Handler Type 10 25 36 java.lang.ClassCastException 10 25 56 java.lang.NullPointerException 10 25 76 36 45 76 56 65 76 76 78 76
Integer x = null;
Object y = new Object();
try {
x = (Integer)y;
System.out.println(x.intValue());
} catch (ClassCastException e) {
System.out.println("y was not an Integer");
} catch (NullPointerException e) {
System.out.println("y was null");
} finally {
System.out.println("finally!");
0: aconst_null 1: astore_ 2: new java.lang.Object 5: dup 6: invokespecial java.lang.Object. ()V 9: astore_ 10: aload_ 11: checkcast java.lang.Integer 14: astore_ 15: getstatic java.lang.System.out Ljava/io/PrintStream; 18: aload_ 19: invokevirtual java.lang.Integer.intValue ()I 22: invokevirtual java.io.PrintStream.println (I)V 25: getstatic java.lang.System.out Ljava/io/PrintStream; 28: ldc "finally!" 30: invokevirtual java.io.PrintStream.println (Ljava/lang/String;)V 33: goto # 36: astore_ 37: getstatic java.lang.System.out Ljava/io/PrintStream; 40: ldc "y was not an Integer" 42: invokevirtual java.io.PrintStream.println (Ljava/lang/String;)V 45: getstatic java.lang.System.out Ljava/io/PrintStream; 48: ldc "finally!" 50: invokevirtual java.io.PrintStream.println (Ljava/lang/String;)V 53: goto # 56: astore_ 57: getstatic java.lang.System.out Ljava/io/PrintStream; 60: ldc "y was null" 62: invokevirtual java.io.PrintStream.println (Ljava/lang/String;)V 65: getstatic java.lang.System.out Ljava/io/PrintStream; 68: ldc "finally!" 70: invokevirtual java.io.PrintStream.println (Ljava/lang/String;)V 73: goto # 76: astore 4 78: getstatic java.lang.System.out Ljava/io/PrintStream; 81: ldc "finally!" 83: invokevirtual java.io.PrintStream.println (Ljava/lang/String;)V 86: aload 4 88: athrow 89: return From To Handler Type 10 25 36 java.lang.ClassCastException 10 25 56 java.lang.NullPointerException 10 25 76 36 45 76 56 65 76 76 78 76
Integer x = null;
Object y = new Object();
try {
x = (Integer)y;
System.out.println(x.intValue());
} catch (ClassCastException e) {
System.out.println("y was not an Integer");
} catch (NullPointerException e) {
System.out.println("y was null");
} finally {
System.out.println("finally!");
0: aconst_null 1: astore_ 2: new java.lang.Object 5: dup 6: invokespecial java.lang.Object. ()V 9: astore_ 10: aload_ 11: checkcast java.lang.Integer 14: astore_ 15: getstatic java.lang.System.out Ljava/io/PrintStream; 18: aload_ 19: invokevirtual java.lang.Integer.intValue ()I 22: invokevirtual java.io.PrintStream.println (I)V 25: getstatic java.lang.System.out Ljava/io/PrintStream; 28: ldc "finally!" 30: invokevirtual java.io.PrintStream.println (Ljava/lang/String;)V 33: goto # 36: astore_ 37: getstatic java.lang.System.out Ljava/io/PrintStream; 40: ldc "y was not an Integer" 42: invokevirtual java.io.PrintStream.println (Ljava/lang/String;)V 45: getstatic java.lang.System.out Ljava/io/PrintStream; 48: ldc "finally!" 50: invokevirtual java.io.PrintStream.println (Ljava/lang/String;)V 53: goto # 56: astore_ 57: getstatic java.lang.System.out Ljava/io/PrintStream; 60: ldc "y was null" 62: invokevirtual java.io.PrintStream.println (Ljava/lang/String;)V 65: getstatic java.lang.System.out Ljava/io/PrintStream; 68: ldc "finally!" 70: invokevirtual java.io.PrintStream.println (Ljava/lang/String;)V 73: goto # 76: astore 4 78: getstatic java.lang.System.out Ljava/io/PrintStream; 81: ldc "finally!" 83: invokevirtual java.io.PrintStream.println (Ljava/lang/String;)V 86: aload 4 88: athrow 89: return From To Handler Type 10 25 36 java.lang.ClassCastException 10 25 56 java.lang.NullPointerException 10 25 76 36 45 76 56 65 76 76 78 76
Integer x = null;
Object y = new Object();
try {
x = (Integer)y;
System.out.println(x.intValue());
} catch (ClassCastException e) {
System.out.println("y was not an Integer");
} catch (NullPointerException e) {
System.out.println("y was null");
} finally {
System.out.println("finally!");
0: aconst_null 1: astore_ 2: new java.lang.Object 5: dup 6: invokespecial java.lang.Object. ()V 9: astore_ 10: aload_ 11: checkcast java.lang.Integer 14: astore_ 15: getstatic java.lang.System.out Ljava/io/PrintStream; 18: aload_ 19: invokevirtual java.lang.Integer.intValue ()I 22: invokevirtual java.io.PrintStream.println (I)V 25: getstatic java.lang.System.out Ljava/io/PrintStream; 28: ldc "finally!" 30: invokevirtual java.io.PrintStream.println (Ljava/lang/String;)V 33: goto # 36: astore_ 37: getstatic java.lang.System.out Ljava/io/PrintStream; 40: ldc "y was not an Integer" 42: invokevirtual java.io.PrintStream.println (Ljava/lang/String;)V 45: getstatic java.lang.System.out Ljava/io/PrintStream; 48: ldc "finally!" 50: invokevirtual java.io.PrintStream.println (Ljava/lang/String;)V 53: goto # 56: astore_ 57: getstatic java.lang.System.out Ljava/io/PrintStream; 60: ldc "y was null" 62: invokevirtual java.io.PrintStream.println (Ljava/lang/String;)V 65: getstatic java.lang.System.out Ljava/io/PrintStream; 68: ldc "finally!" 70: invokevirtual java.io.PrintStream.println (Ljava/lang/String;)V 73: goto # 76: astore 4 78: getstatic java.lang.System.out Ljava/io/PrintStream; 81: ldc "finally!" 83: invokevirtual java.io.PrintStream.println (Ljava/lang/String;)V 86: aload 4 88: athrow 89: return From To Handler Type 10 25 36 java.lang.ClassCastException 10 25 56 java.lang.NullPointerException 10 25 76 36 45 76 56 65 76 76 78 76
Integer x = null;
Object y = new Object();
try {
x = (Integer)y;
System.out.println(x.intValue());
} catch (ClassCastException e) {
System.out.println("y was not an Integer");
} catch (NullPointerException e) {
System.out.println("y was null");
} finally {
System.out.println("finally!");