statements and expressions

29
CS884 (Prasad) java7ExprSt 1 Statements and Expressions Execution

Upload: garth-burris

Post on 01-Jan-2016

29 views

Category:

Documents


0 download

DESCRIPTION

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 - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Statements and Expressions

CS884 (Prasad) java7ExprSt 1

Statements and Expressions

Execution

Page 2: Statements and Expressions

CS884 (Prasad) java7ExprSt 2

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.

Page 3: Statements and Expressions

CS884 (Prasad) java7ExprSt 3

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.

Page 4: Statements and Expressions

CS884 (Prasad) java7ExprSt 4

• 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.

Page 5: Statements and Expressions

CS884 (Prasad) java7ExprSt 5

Execution

Page 6: Statements and Expressions

CS884 (Prasad) java7ExprSt 6

• 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.

Page 7: Statements and Expressions

CS884 (Prasad) java7ExprSt 7

– 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.

Page 8: Statements and Expressions

CS884 (Prasad) java7ExprSt 8

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.

Page 9: Statements and Expressions

CS884 (Prasad) java7ExprSt 9

Page 10: Statements and Expressions

CS884 (Prasad) java7ExprSt 10

Finalization

Page 11: Statements and Expressions

CS884 (Prasad) java7ExprSt 11

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.

Page 12: Statements and Expressions

CS884 (Prasad) java7ExprSt 12

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(); } }}

Page 13: Statements and Expressions

CS884 (Prasad) java7ExprSt 13

Exampleclass Resurrection { public static Resurrection rs; Integer ii; Resurrection (Integer j) { ii = j; } protected void finalize () throws Throwable { super.finalize(); rs = this; } . . . }

Page 14: Statements and Expressions

CS884 (Prasad) java7ExprSt 14

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 }

Page 15: Statements and Expressions

CS884 (Prasad) java7ExprSt 15

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 )

Page 16: Statements and Expressions

CS884 (Prasad) java7ExprSt 16

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.

Page 17: Statements and Expressions

CS884 (Prasad) java7ExprSt 17

Page 18: Statements and Expressions

CS884 (Prasad) java7ExprSt 18

Extended Example

class P {...}class Q extends P {... public void finalize() {…}}

p

Pnode 1

Pnode 5Q

node 2Q

node 3Q

node 4

Page 19: Statements and Expressions

CS884 (Prasad) java7ExprSt 19

• 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)

pP

n 1P

n 5Qn 2

Qn 3

Qn 4

Page 20: Statements and Expressions

CS884 (Prasad) java7ExprSt 20

• 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.

pP

n 1P

n 5Qn 2

Qn 3

Qn 4

Page 21: Statements and Expressions

CS884 (Prasad) java7ExprSt 21

• 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

pP

n 1P

n 5Qn 2

Qn 3

Qn 4

Page 22: Statements and Expressions

CS884 (Prasad) java7ExprSt 22

• 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

pP

n 1P

n 5Qn 2

Qn 3

Qn 4

Page 23: Statements and Expressions

CS884 (Prasad) java7ExprSt 23

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)

Page 24: Statements and Expressions

CS884 (Prasad) java7ExprSt 24

• 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)

Page 25: Statements and Expressions

CS884 (Prasad) java7ExprSt 25

Odds and Ends

Page 26: Statements and Expressions

CS884 (Prasad) java7ExprSt 26

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• new int [3] [2]

– Two-dimensional array: new (int [3] [2])– Array access: (new int [3]) [2]

Page 27: Statements and Expressions

CS884 (Prasad) java7ExprSt 27

• (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.

Page 28: Statements and Expressions

CS 884 (Prasad) Java Interfaces 28

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.

Page 29: Statements and Expressions

CS 884 (Prasad) Java Interfaces 29

Minor Naming Inconsistencies in APIs

• size() in Vector, ByteArrayOutputStream

• length() in String

• getLength() in DataGramPacket

• countItems() in List

• countTokens() in StringTokenizer

• countComponents() in Container