290 likes | 436 Views
Statements and Expressions. Execution. Statements. Essentially C/C++ syntax Dangling-else ambiguity “ if ( C ) if ( D ) S1 else S2 ” resolved as “ if ( C ) { if ( D ) S1 else S2 } ”. Compiler checks for: Uninitialized variables
E N D
Statements and Expressions Execution java7ExprSt
Statements • Essentially C/C++ syntax • Dangling-else ambiguity “ if(C)if(D) S1 else S2 ” resolved as “ if(C) { if(D) S1 else S2 } ”. • Compiler checks for: • Uninitialized variables • “int i = i;” is illegal, but “int i = (i = 3);” is legal. • Unreachable statements • “while (false) i = 3;” is illegal. • “if (false) i = 3;” is legal. java7ExprSt
Expression • Expression Statements • Assignment, post/pre increment/decrement. • Method invocation, Class instance creation. • Expression evaluation results in a variable, a value, or nothing (void). • For reference type expressions, the actual class of referenced object is constrained. The actual class of object determines program execution in: • Method invocation, instanceof. • Cast, Assignment to array components, Exception handling. java7ExprSt
Operands of operators are evaluated from left to right, respecting parentheses and precedence, for portability. • “inti =2; i = (i = 3) * i;” binds 9 to i. • “inti =2; i *= (i = 3);” binds 6 to i. • Assignment operator “=” is right associative. • Every operand of an operator is evaluated before any part of the operation is performed (exceptions: &&, ||, and ?:). • Argument lists in call and array dimension exprs. are evaluated from left to right. java7ExprSt
Execution java7ExprSt
Virtual Machine Start-up • java Test 1 2 abc • Loading a class • Class Loader loads binary representation (byte code file) of a class, and constructs a Class object. • May throw ClassCircularityError, ClassFormatError, NoClassDefFound, etc • Linking • Verification, Preparation, Resolution (optional) • Initialization • Runs class variable initializers and static intializer. • Interfaces initialized on “active” use. • May involve loading, linking, and initializing ancestor classes. java7ExprSt
Verification of byte codes • Structural and type correctness of instructions • Branches to instruction start, etc • May throw VerifyError • Run-time stack growth • Preparation • Allocation of static storage and internal data structures • Resolving Symbolic References • Checks symbolic references to guard against possible incompatible changes since compilation • May throw IllegalAccessError, InstantiationError, NoSuchFieldError, NoSuchMethodError. java7ExprSt
Other Activities • Creation of an instance of a class • Constructor invocation • Destruction of an instance • Finalization (finalize method) • Destruction of class objects removed in JLS update as redundant and rarely used feature. java7ExprSt
Finalization java7ExprSt
Special Method in classObject protected voidfinalize()throwsThrowable { ...; super.finalize() ; ... } • This method is executed to reclaim non-Java resources, without waiting until garbage collection, to prevent resource leakage. • It is guaranteed to be called by JVM at most once automatically. • Typically the code for finalize() contains a call to finalize() in parent. java7ExprSt
Typical Use public class ProcessFile { private FileReader file; . . . public synchronized void close() throws IOException { if (file != null) { file.close(); file = null; } } protected void finalize() throws Throwable { try { close(); } finally { super.finalize(); } } } java7ExprSt
Example classResurrection{ public staticResurrection rs; Integerii; Resurrection (Integer j) { ii = j; } protected voidfinalize () throwsThrowable { super.finalize(); rs = this; } . . . } java7ExprSt
public static void main ( String [] args ) throws Throwable { newResurrection(newInteger("666")); // f-reachable, unfinalized Integerm = newInteger(666); Resurrection rm = newResurrection(m); // rm : reachable, unfinalized rm.finalize(); rm.finalize(); // no effect on state rm = null; m = null; // unreachable, unfinalized // JVM :: finalizer-reachable, finalizable System.runFinalization(); // reachable finalizable/finalized // unreachable finalized } java7ExprSt
Partition of Instances • W.r.t Reachability • Reachable • from variables in live threads, starting from main()or run() • Finalizer-Reachable • from instances in “finalize-queue”, eventually reachable from this in finalize() • Unreachable • W.r.t Finalize-queue • Unfinalized (·yet to enter the queue ) • Finalizable (·in the queue ) • Finalized (·off the queue ) java7ExprSt
FSM to model state of an instance • States = {Reachable, F-Reachable, Unreachable} x {Unfinalized, Finalizable, Finalized } • (Reachable, Unfinalized) : Start • (Unreachable, Finalized) : Final • (Unreachable, Finalizable) : Inconsistent • Transitions-on ( “garbage creators” ) • Assignment, call to finalize(), method return etc. • Thread death, JVM’s action on “finalize-queue” etc. java7ExprSt
p P node 1 Q node 2 Q node 3 Q node 4 P node 5 Extended Example class P {...} class Q extends P {... public void finalize() {…} } java7ExprSt
p P n 1 Q n 2 Q n 3 Q n 4 P n 5 • Step I: Initially, • nodes 1-5 : (reachable, unfinalized) • Step II: p = null • node 1: (unreachable, unfinalized) • nodes 2-5: (f-reachable, unfinalized) • Step III: Reclaim node 1 • Step IV: Set up to finalize nodes 3 and 4 • node 2: (f-reachable, unfinalized) • nodes 3-4: (f-reachable, finalizable) • node 5: (f-reachable, unfinalized) java7ExprSt
p P n 1 Q n 2 Q n 3 Q n 4 P n 5 • Step V: Run finalizer for node 3 • node 2: (f-reachable, unfinalized) • node 3: (reachable, finalized) • node 4: (reachable, finalizable) • node 5: (reachable, unfinalized) • Step VI: • node 2: (f-reachable, unfinalized) • node 3: (f-reachable, finalized) • node 4: (f-reachable, finalizable) • node 5: (f-reachable, unfinalized) • node 3 cannot be collected as it is still linked to node 2. java7ExprSt
p P n 1 Q n 2 Q n 3 Q n 4 P n 5 • Step VII: Set up and run finalizer for node 2 • nodes 2-3: (reachable, finalized) • node 4: (reachable, finalizable) • node 5: (reachable, unfinalized) • Step VIII: • nodes 2-3: (unreachable, finalized) • node 4: (f-reachable, finalizable) • node 5: (f-reachable, unfinalized) • Step IX: Reclaim nodes 2 and 3 java7ExprSt
p P n 1 Q n 2 Q n 3 Q n 4 P n 5 • Step X: Run finalizer for node 4 • node 4: (reachable, finalized) • node 5: (reachable, unfinalized) • Step XI: • node 4: (unreachable, finalized) • node 5: (unreachable, unfinalized) • Step XII: Reclaim nodes 4 and 5 • trivial finalizer for node 5 not run java7ExprSt
General remarks on Transitions • A reachable object becomes unreachable if there is no reference to it from a variable and it has no “predecessor” object. (E, F) • A (temporarily reachable) object becomes f-reachable if its “predecessor” is finalizable. (B, C, D) • An f-reachable object becomes reachable when its finalize()or its “predecessor”’s finalize()is executed. (L, M, N) java7ExprSt
JVM marks unfinalized and notreachable objects f-reachable and finalizable. (G, H) • Ifan object isunreachable (notf-reachable),andfinalize() is not overridden, then JVM can mark it as finalized. (Optimization) • JVM invokes finalize() on a finalizable object, after marking it finalized. (J, K) • Unreachable and finalized objects can be garbage collected. (I) • A newly created object can be reachable, f-reachable or unreachable. (A, O, I) java7ExprSt
Odds and Ends java7ExprSt
Parsing Issues: LALR(1) • Ambiguity (for one-lookahead) • Cast vs Parenthesized array access • ( z [ ] ) vs ( z [ 3 ] ) • Cast vs binary operator expression • (p) + q vs (p) +q • (p)++ q vs (p)++ • Array creation expression • newint[3] [2] • Two-dimensional array: new (int[3] [2]) • Array access: (new int[3]) [2] java7ExprSt
(p) + q • (p) ++ q vs (p)++ • C/C++ parsers resolve ambiguity between type cast operator vs operand for binary + or unary ++ by semantic analysis to determine if p is a type or a variable. • In Java, for +/++ to be unary and numeric, p must be a keyword for casting an integer subtype. If operators could be overloaded, it would complicate matters here. java7ExprSt
Irregularities in Declarations? • Local variable vs formal parameter (definitions) • int i,j; =int i; int j; • f(int i, j); ¹f(int i, int j); • Constructor signature vs method signature • constructor “return type” omitted, not void • Multiple declarations (not definitions) • Import and Inheritance of the same field multiple times permitted. • Repeating an interface name in implement-clause illegal. Java Interfaces
Minor Naming Inconsistencies in APIs • size() in Vector, ByteArrayOutputStream • length() in String • getLength() in DataGramPacket • countItems() in List • countTokens() in StringTokenizer • countComponents() in Container Java Interfaces