Statements and Expressions - PowerPoint PPT Presentation

About This Presentation
Title:

Statements and Expressions

Description:

Dangling-else ambiguity ' if (C) if (D) S1 else S2 ' resolved as ' if (C) { if (D) S1 else S2 } ... Expression evaluation results in a variable, a value, or ... – PowerPoint PPT presentation

Number of Views:32
Avg rating:3.0/5.0
Slides: 30
Provided by: TKPr6
Category:

less

Transcript and Presenter's Notes

Title: Statements and Expressions


1
Statements and Expressions
  • Execution

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.

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.

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.

5
Execution
6
  • 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.

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.

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.

9
(No Transcript)
10
Finalization
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.

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

13
Example
  • class Resurrection
  • public static Resurrection rs
  • Integer ii
  • Resurrection (Integer j)
  • ii j
  • protected void finalize () throws
    Throwable
  • super.finalize()
  • rs this
  • . . .

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

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 )

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, JVMs action on finalize-queue
    etc.

17
(No Transcript)
18
Extended Example
class P ... class Q extends P ... public
void finalize()
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)

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.

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

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

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 predecessors finalize() is
    executed. (L, M, N)

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)

25
Odds and Ends
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

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.

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.

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
Write a Comment
User Comments (0)
About PowerShow.com