CS884 (Prasad)java7ExprSt1 Statements and Expressions Execution.

Slides:



Advertisements
Similar presentations
Core Java Lecture 4-5. What We Will Cover Today What Are Methods Scope and Life Time of Variables Command Line Arguments Use of static keyword in Java.
Advertisements

METHOD OVERRIDING 1.Sub class can override the methods defined by the super class. 2.Overridden Methods in the sub classes should have same name, same.
Written by: Dr. JJ Shepherd
Java Virtual Machine (JVM). Lecture Objectives Learn about the Java Virtual Machine (JVM) Understand the functionalities of the class loader subsystem.
CERTIFICATION OBJECTIVES Use Class Members Develop Wrapper Code & Autoboxing Code Determine the Effects of Passing Variables into Methods Recognize when.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Inheritance and Polymorphism.
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
JVM-1 Java Virtual Machine Reading Assignment: Chapter 1: All Chapter 3: Sections.
Road Map Introduction to object oriented programming. Classes
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
JVM-1 Java Virtual Machine Reading Assignment: Chapter 1: All Chapter 3: Sections.
Java Virtual Machine (JVM). Lecture Objectives Learn about the Java Virtual Machine (JVM) Understand the functionalities of the class loader subsystem.
JVM-1 Introduction to Java Virtual Machine. JVM-2 Outline Java Language, Java Virtual Machine and Java Platform Organization of Java Virtual Machine Garbage.
Scott Grissom, copyright 2004Ch 3: Java Features Slide 1 Why Java? It is object-oriented provides many ready to use classes platform independent modern.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
1 Inheritance and Polymorphism. 2 Motivations Suppose you will define classes to model circles, rectangles, and triangles. These classes have many common.
Data Abstraction and Object- Oriented Programming CS351 – Programming Paradigms.
Unit 061 Java Virtual Machine (JVM) What is Java Virtual Machine? The Class Loader Subsystem Linking oVerification oPreparation oResolution Class Initialization.
Polymorphism. Lecture Objectives To understand the concept of polymorphism To understand the concept of static or early binding To understand the concept.
1 Further OO Concepts II – Java Program at run-time Overview l Steps in Executing a Java Program. l Loading l Linking l Initialization l Creation of Objects.
Java Security Updated May Topics Intro to the Java Sandbox Language Level Security Run Time Security Evolution of Security Sandbox Models The Security.
Copyright © 2003 ProsoftTraining. All rights reserved. Sun Certified Java Programmer Exam Preparation Guide.
Java Methods By J. W. Rider. Java Methods Modularity Declaring methods –Header, signature, prototype Static Void Local variables –this Return Reentrancy.
OOP Languages: Java vs C++
Java Security. Topics Intro to the Java Sandbox Language Level Security Run Time Security Evolution of Security Sandbox Models The Security Manager.
Programming Languages and Paradigms Object-Oriented Programming.
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
1 Inheritance and Polymorphism Chapter 9. 2 Polymorphism, Dynamic Binding and Generic Programming public class Test { public static void main(String[]
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes.
Java and C++, The Difference An introduction Unit - 00.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes.
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
CSM-Java Programming-I Spring,2005 Objects and Classes Overview Lesson - 1.
1 Review of Java Higher Level Language Concepts –Names and Reserved Words –Expressions and Precedence of Operators –Flow of Control – Selection –Flow of.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Lecture 10 : Introduction to Java Virtual Machine
2000 Jordan Anastasiade. All rights reserved. 1 Class In this lesson you will be learning about: Class. Inheritance. Polymorphism. Nested and.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
C# Classes and Inheritance CNS 3260 C#.NET Software Development.
Java Basics Opening Discussion zWhat did we talk about last class? zWhat are the basic constructs in the programming languages you are familiar.
Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the.
 Objects versus Class  Three main concepts of OOP ◦ Encapsulation ◦ Inheritance ◦ Polymorphism  Method ◦ Parameterized ◦ Value-Returning.
OOP in C++ CS 124. Program Structure C++ Program: collection of files Source (.cpp) files be compiled separately to be linked into an executable Files.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 9 Inheritance and.
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
SOEN 343 Software Design Section H Fall 2006 Dr Greg Butler
CSI 3125, Preliminaries, page 1 Class. CSI 3125, Preliminaries, page 2 Class The most important thing to understand about a class is that it defines a.
Semantic Analysis II Type Checking EECS 483 – Lecture 12 University of Michigan Wednesday, October 18, 2006.
Classes, Interfaces and Packages
CIS 200 Test 01 Review. Built-In Types Properties  Exposed “Variables” or accessible values of an object  Can have access controlled via scope modifiers.
CS 884 (Prasad)Java Types1 Language Specification Semantics of constructs. –Definition and use of name-value bindings: Name Resolution. Soundness : No.
© 2006 Pearson Addison-Wesley. All rights reserved 1-1 Chapter 1 Review of Java Fundamentals.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
5.1 Basics of defining and using classes A review of class and object definitions A class is a template or blueprint for an object A class defines.
Reference Types CSE301 University of Sunderland Harry R Erwin, PhD.
BY:- TOPS Technologies
RealTimeSystems Lab Jong-Koo, Lim
MT311 Java Application Development and Programming Languages Li Tak Sing( 李德成 )
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Polymorphism in Methods
Chapter 11 Inheritance and Polymorphism
Java Primer 1: Types, Classes and Operators
Java Programming Language
Lecture 2: Data Types, Variables, Operators, and Expressions
Chapter 3 Assignment Statement
Chapter 9 Inheritance and Polymorphism
Extending Classes.
Java Programming Language
Presentation transcript:

CS884 (Prasad)java7ExprSt1 Statements and Expressions Execution

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.

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.

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.

CS884 (Prasad)java7ExprSt5 Execution

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.

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.

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.

CS884 (Prasad)java7ExprSt9

CS884 (Prasad)java7ExprSt10 Finalization

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.

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

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; }... }

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 }

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 )

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.

CS884 (Prasad)java7ExprSt17

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

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

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

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

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

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)

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)

CS884 (Prasad)java7ExprSt25 Odds and Ends

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]

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.

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.

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