Java and the Java Virtual Machine 3. The imperative Core Java I of Java Pslab 오민경.

Slides:



Advertisements
Similar presentations
Decision Structures - If / Else If / Else. Decisions Often we need to make decisions based on information that we receive. Often we need to make decisions.
Advertisements

CH4.1 Type Checking Md. Fahim Computer Engineering Department Jamia Millia Islamia (A Central University) New Delhi –
STRING AN EXAMPLE OF REFERENCE DATA TYPE. 2 Primitive Data Types  The eight Java primitive data types are:  byte  short  int  long  float  double.
IT 325 OPERATING SYSTEM C programming language. Why use C instead of Java Intermediate-level language:  Low-level features like bit operations  High-level.
Intermediate Code Generation
Programming Languages and Paradigms The C Programming Language.
Control Structures Any mechanism that departs from straight-line execution: –Selection: if-statements –Multiway-selection: case statements –Unbounded iteration:
Fundamental Programming Structures in Java: Control Flow, Arrays and Vectors.
Expressions and Statements. 2 Contents Side effects: expressions and statements Expression notations Expression evaluation orders Conditional statements.
Compiler Construction
Java Syntax Part I Comments Identifiers Primitive Data Types Assignment.
Constants and Variables  Memory cells used in program  Called constants and variables  Identifiers for constants and variables should be declared before.
Introduction to Programming with Java, for Beginners Primitive Types Expressions Statements Variables Strings.
Differences between Java and C CS-2303, C-Term Differences between Java and C CS-2303, System Programming Concepts (Slides include materials from.
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
Denotational Semantics Syntax-directed approach, generalization of attribute grammars: –Define context-free abstract syntax –Specify syntactic categories.
Day 4 Objectives Constructors Wrapper Classes Operators Java Control Statements Practice the language.
Java Building Elements Lecture 2 Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung University
Imperative Programming
 Value, Variable and Data Type  Type Conversion  Arithmetic Expression Evaluation  Scope of variable.
7-1 Chapter 7: Expressions and Assignment Statements Arithmetic Expressions –the fundamental means of specifying computations in a programming language.
C H A P T E R S E V E N Expressions and Assignment Statements.
The object-oriented extension Java o of Java c (Java and the Java Virtual Machine ch. 5) PSLab 문세원.
Chapter 2 Basic Elements of Java. Chapter Objectives Become familiar with the basic components of a Java program, including methods, special symbols,
DiagrammaticRepresentation Iteration Construct False True Condition Exit from Statement (s) loop Sequence construct Selection construct Statement 1 Statement.
JAVA 0. HAFTA Algorithms FOURTH EDITION Robert Sedgewick and Kevin Wayne Princeton University.
Java Simple Types CSIS 3701: Advanced Object Oriented Programming.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
Expressions An expression is a series of variables, operators, and method calls (constructed according to the syntax of the language) that evaluates to.
Introduction to Programming Languages S1.3.1Bina © 1998 Liran & Ofir Introduction to Programming Languages Programming in C.
Switch Statements Comparing Exact Values. The Switch Statement: Syntax The switch statement provides another way to decide which statement to execute.
CPS 506 Comparative Programming Languages Syntax Specification.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
J AVA P ROGRAMMING 2 C H 03: C ONTROL STATEMENTS if, for loop (review) switch, while, do while break, continue Fall Java Programming.
Java Programming, Second Edition Chapter Two Using Data Within a Program.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
Java Nuts and Bolts Variables and Data Types Operators Expressions Control Flow Statements Arrays and Strings.
 Control Flow statements ◦ Selection statements ◦ Iteration statements ◦ Jump statements.
CPS120: Introduction to Computer Science Variables and Constants.
Spring 2009 Programming Fundamentals I Java Programming XuanTung Hoang Lecture No. 8.
Chapter 1 Java Programming Review. Introduction Java is platform-independent, meaning that you can write a program once and run it anywhere. Java programs.
LESSON 5 – Assignment Statements JAVA PROGRAMMING.
4. The procedural extension Java c of Java I pslab 김윤경.
Session 2 Operators, Decisions and Loops. Objectives Operators Casting data Decision marking structures Loops break, continue, return.
JAVA Programming (Session 2) “When you are willing to make sacrifices for a great cause, you will never be alone.” Instructor: รัฐภูมิ เถื่อนถนอม
1 Lecture # 2. * Introducing Programming with an Example * Identifiers, Variables, and Constants * Primitive Data Types * Byte, short, int, long, float,
Today… Preparation for doing Assignment 1. Invoking methods overview. Conditionals and Loops. Winter 2016CMPE212 - Prof. McLeod1.
Review A program is… a set of instructions that tell a computer what to do. Programs can also be called… software. Hardware refers to… the physical components.
Chapter 7 Control Structures. Java has very flexible three looping mechanisms. You can use one of the following three loops:  while Loop  do...while.
Expressions and Assignment Statements
Information and Computer Sciences University of Hawaii, Manoa
Expressions and Assignment Statements
The Machine Model Memory
CIS3931 – Intro to JAVA Lecture Note Set 2 17-May-05.
Expressions and Assignment Statements
Java Primer 1: Types, Classes and Operators
Object Oriented Programming
Expressions and Assignment Statements
Imperative languages Most familiar: computation modifies store
Java Programming: From Problem Analysis to Program Design, 4e
Java - Data Types, Variables, and Arrays
CSC215 Lecture Control Flow.
Sridhar Narayan Java Basics Sridhar Narayan
Differences between Java and C
Java Programming Review 1
Compiler Construction
Java’s Central Casting
Type Conversion It is a procedure of converting one data type values into another data type In C programming language we are having two types of type conversion.
CSC215 Lecture Control Flow.
Compiler Construction
Presentation transcript:

Java and the Java Virtual Machine 3. The imperative Core Java I of Java Pslab 오민경

Static semantics of Java I Primitive type  Numeric type Integral type : byte, short, int long, char Floating point type : float, double  Conditional type boolean Literal  External representations of values of primitive type  i.e. 11, 3.14f, true …

Static semantics of Java I Relation ‘ ⊆ ’  A ⊆ B A is subtype of B ( A, B : Type) exist a widening primitive conversion from A to B Each value of A can used as a value of B, however, information may be lost.  Relation ‘ ⊆ ’ Byte ⊆ short ⊆ int ⊆ long ⊆ float ⊆ double, char ⊆ int Reflexive : A ⊆ A Transitive : if A ⊂ B and B ⊂ C, then A ⊂ C

Syntax of Java I Domain  Domains (or universes) : words beginning with a capital letter i.e. Exp … expressions, Stm … statements  Elements of ‘Dom’ : ‘dom’ Local variable  Local variable declaration : ‘A loc;’  The scope of loc consists of the statements following the variable declaration in the block.

Type checking of Java I Position  Position : small Greek letters α, β, γ. i.e. α exp, β stm  The universe of positions is called ‘Pos’. Compiler verification : type checking  The compiler has to verify that a program is well-typed.  As a result of pasing and elaboration the parse tree annotated with type information : T(α).  During compilation and type checking explicit unary conversion operators (type cast) are inserted at places where they are necessary.

Transition rules for Java I execJava I = execJavaExp I execJava Stm I execJavaExp I  All expressions are evaluated from innermost to outermost.  For this purpose, the current control is transferred, by updating pos. from unevaluated expressions to the appropriate subexpressions. until an atom (a literal or a variable) is reached.

Transition rules for Java I execJava Stm I  The syntactical structure of the statement to be computed by transferring.  For this purpose through updates of pos, the current control from structured statements to the appropriate substatements until the current statement has been computed normally or abrupts the computation. Abruption : Jump statement (break, continue)  Not a labeled phrase, not degree labeled phrase : Abruption is propagated upwards.  Break(lab b ) : execution proceeds normally at the next phrase of the target  Continue(lab c ) : execution proceeds with the next iteration of the corresponding while statement.

Derived language constructs Java constructs that can be syntactically reduced to the core language  ++loc : loc=(A)(loc+1)  --loc : loc=(A)(loc-1)  If (exp) stm : if (exp) stm else; The ‘if statement without else’ suffers from the so- called ‘dangling else problem’  If (exp1) if (exp2) stm1 else stm2 If (exp1) { if (exp2) stm1 else stm2 } => O If (exp1) { if (exp2) stm1 } else stm2 => X

Syntax of Java Exp := Lit | Loc | Uop Exp | Exp Bop Exp | Exp ? Exp : Exp | Asgn Asgn := Loc = Exp Stm := ; | Asgn; | Lab : Stm | break Lab; | continue Lab; | if (Exp) Stm else Stm | while (Exp) Stm | Block Block := {Bstm1 … Bstmn} Bstm := Type Loc; | Stm Phrase := Exp | Bstm | Val | Abr | Norm

Execution of Java I expressions execJavaExpI = case context(pos) of lit → yield(JLS(lit)) loc → yield(locals(loc)) uop α exp → pos := α uop ► val → yieldUp(JLS(uop, val)) α exp1 bop β exp2 → pos := α ► val bop β exp → pos := β α val1 bop ► val2 → if ¬(bop 2 divMod ∧ isZero(val2)) then yieldUp(JLS(bop, val1, val2)) loc = α exp → pos := α loc = ► val → locals := locals + {(loc, val)} yieldUp(val) α exp0 ? β exp1 : γ exp2 → pos := α ► val ? β exp1 : γ exp2 → if val then pos := β else pos := γ α True ? ► val : γ exp → yieldUp(val) α False ? β exp : ► val → yieldUp(val)

Execution of Java I statements execJavaStmI = case context(pos) of ; → yield(Norm) α exp; → pos := α ► val; → yieldUp(Norm) break lab; → yield(Break(lab)) continue lab; → yield(Continue(lab)) lab : α stm → pos := α lab : ► Norm → yieldUp(Norm) lab : ► Break(lab b ) → if lab = lab b then yieldUp(Norm) else yieldUp(Break(lab b )) lab : ► Continue(lab c ) → if lab = lab c then yield(body/pos) else yieldUp(Continue(lab c )) phrase( ► abr) → if pos ≠ firstPos ∧ propagatesAbr(restbody/up(pos)) then yieldUp(abr)

Execution of Java I statements { } → yield(Norm) { α1 stm 1 … αn stm n } → pos := α 1 { α1 Norm … ► Norm} → yieldUp(Norm) { α1 Norm … ► Norm αi+1 stm i+1 … αn stm n } → pos := α i+1 if ( α exp) β stm1 else γ stm2 → pos := α if ( ► val) β stm1 else γ stm2 → if val then pos := β else pos := γ if ( α True) ► Norm else γ stm → yieldUp(Norm) if ( α False) β stm else ► Norm → yieldUp(Norm) while ( α exp) β stm → pos := α while ( ► val) β stm → if val then pos := β else yieldUp(Norm) while ( α True) ► Norm → yieldUp(body/up(pos)) Type x ; → yield(Norm)