Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS884 (Prasad)java7ExprSt1 Statements and Expressions Execution.

Similar presentations

Presentation on theme: "CS884 (Prasad)java7ExprSt1 Statements and Expressions Execution."— Presentation transcript:

1 CS884 (Prasad)java7ExprSt1 Statements and Expressions Execution

2 CS884 (Prasad)java7ExprSt2 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.

3 CS884 (Prasad)java7ExprSt3 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.

4 CS884 (Prasad)java7ExprSt4 Operands of operators are evaluated from left to right, respecting parentheses and precedence, for portability. “int i =2; i = (i = 3) * i;” binds 9 to i. “int i =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.

5 CS884 (Prasad)java7ExprSt5 Execution

6 CS884 (Prasad)java7ExprSt6 Virtual Machine Start-up java Test 1 2 abc Loading a class Class LoaderClass 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.

7 CS884 (Prasad)java7ExprSt7 –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.

8 CS884 (Prasad)java7ExprSt8 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.

9 CS884 (Prasad)java7ExprSt9

10 CS884 (Prasad)java7ExprSt10 Finalization

11 CS884 (Prasad)java7ExprSt11 Special Method in class Object protected void finalize() throws Throwable {...; 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.

12 CS884 (Prasad)java7ExprSt12 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(); } } }

13 CS884 (Prasad)java7ExprSt13 Example class Resurrection { public static Resurrection rs; Integer ii; Resurrection (Integer j) { ii = j; } protected void finalize () throws Throwable { super.finalize(); rs = this; }... }

14 CS884 (Prasad)java7ExprSt14 public static void main ( String [] args ) throws Throwable { new Resurrection(new Integer("666")); // f-reachable, unfinalized Integer m = new Integer(666); Resurrection rm = new Resurrection(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 }

15 CS884 (Prasad)java7ExprSt15 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 )

16 CS884 (Prasad)java7ExprSt16 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.

17 CS884 (Prasad)java7ExprSt17

18 CS884 (Prasad)java7ExprSt18 Extended Example class P {...} class Q extends P {... public void finalize() {…} } p P node 1 P node 5 Q node 2 Q node 3 Q node 4

19 CS884 (Prasad)java7ExprSt19 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) p P n 1 P n 5 Q n 2 Q n 3 Q n 4

20 CS884 (Prasad)java7ExprSt20 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. p P n 1 P n 5 Q n 2 Q n 3 Q n 4

21 CS884 (Prasad)java7ExprSt21 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 p P n 1 P n 5 Q n 2 Q n 3 Q n 4

22 CS884 (Prasad)java7ExprSt22 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 p P n 1 P n 5 Q n 2 Q n 3 Q n 4

23 CS884 (Prasad)java7ExprSt23 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)

24 CS884 (Prasad)java7ExprSt24 JVM marks unfinalized and not reachable objects f-reachable and finalizable. (G, H) –If an object is unreachable (not f-reachable), and finalize() 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)

25 CS884 (Prasad)java7ExprSt25 Odds and Ends

26 CS884 (Prasad)java7ExprSt26 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 expressio n new int [3] [2] –Two-dimensional array: new (int [3] [2]) –Array access: (new int [3]) [2]

27 CS884 (Prasad)java7ExprSt27 (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.

28 CS 884 (Prasad)Java Interfaces28 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.

29 CS 884 (Prasad)Java Interfaces29 Minor Naming Inconsistencies in APIs size() in Vector, ByteArrayOutputStream length() in String getLength() in DataGramPacket countItems() in List countTokens() in StringTokenizer countComponents() in Container

Download ppt "CS884 (Prasad)java7ExprSt1 Statements and Expressions Execution."

Similar presentations

Ads by Google