MT311 Java Application Development and Programming Languages Li Tak Sing ( 李德成 )

Slides:



Advertisements
Similar presentations
Intermediate Code Generation
Advertisements

Names and Bindings.
COP4020 Programming Languages Expression and assignment Prof. Xin Yuan.
ISBN Chapter 7 Expressions and Assignment Statements.
Types and Arithmetic Operators
Data Types in Java Data is the information that a program has to work with. Data is of different types. The type of a piece of data tells Java what can.
Chapter 5: Elementary Data Types Properties of types and objects –Data objects, variables and constants –Data types –Declarations –Type checking –Assignment.
COEN Expressions and Assignment
Copyright © by Curt Hill Expressions and Assignment Statements Part 2.
Type Checking.
Compiler Construction
ISBN Chapter 7 Expressions and Assignment Statements.
1 Chapter 7: Expressions Expressions are the fundamental means of specifying computations in a programming language To understand expression evaluation,
Chapter 7 Expressions and Assignment Statements. Copyright © 2007 Addison-Wesley. All rights reserved. 1–2 Arithmetic Expressions Arithmetic evaluation.
ISBN Chapter 7 Expressions and Assignment Statements.
ISBN Lecture 07 Expressions and Assignment Statements.
Expressions, Evaluation and Assignments
MT311 Java Application Programming and Programming Languages Li Tak Sing ( 李德成 )
Expressions & Assignments One of the original intentions of computer programs was the evaluation of mathematical expressions –languages would inherit much.
MT311 Java Programming and Programming Languages Li Tak Sing ( 李德成 )
MT311 Java Application Development and Programming Languages Li Tak Sing( 李德成 )
Copyright © 1998 by Addison -Wesley Longman, Inc. 1 Chapter 6 Arithmetic Expressions - Their evaluation was one of the motivations for the development.
1-1 University of Hail College of Computer Science and Engineering Department of computer Science and Software Engineering Course: ICS313: Fundamentals.
7-1 Chapter 7: Expressions and Assignment Statements Arithmetic Expressions –the fundamental means of specifying computations in a programming language.
Expressions creating information. topics  operators: precedence, associativity, parentheses, overloading  operands: side-effects, coercion  arithmetic,
Ryan Chu. Arithmetic Expressions Arithmetic expressions consist of operators, operands, parentheses, and function calls. The purpose is to specify an.
Chapter 7 Expressions and Assignment Statements. Copyright © 2007 Addison-Wesley. All rights reserved. 1–2 Arithmetic Expressions Arithmetic evaluation.
COMP4730/2002/lec7/H.Melikian Arithmetic Expressions Overloaded Operators Type Conversations Relational and Boolean Expressions Short Circuit Evaluation.
CS 363 Comparative Programming Languages Expressions and Assignment Statements.
C H A P T E R S E V E N Expressions and Assignment Statements.
CSC3315 (Spring 2009)1 CSC 3315 Programming Languages Hamid Harroud School of Science and Engineering, Akhawayn University
Names Variables Type Checking Strong Typing Type Compatibility 1.
5-1 Chapter 5: Names, Bindings, Type Checking, and Scopes Variables The Concept of Binding Type Checking Strong Typing Type Compatibility Scope and Lifetime.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 CST 221 OBJECT ORIENTED PROGRAMMING(OOP) ( 2 CREDITS.
ISBN Chapter 7 Expressions and Assignment Statements.
CSI 3120, Expressions and assignment, page 1 Expressions and assignment Points for discussion Arithmetic expressions Overloading Logical expressions Assignment.
1 Records Record aggregate of data elements –Possibly heterogeneous –Elements/slots are identified by names –Elements in same fixed order in all records.
Arithmetic Expressions
Expressions and Assignment Statements
ISBN Chapter 7 Expressions and Assignment Statements.
Chapter 7 © 1998 by Addison -Wesley Longman, Inc Arithmetic Expressions - Their evaluation was one of the motivations for the development of the.
MT311 Java Application Development and Programming Languages Li Tak Sing ( 李德成 )
Types(1). Lecture 52 Type(1)  A type is a collection of values and operations on those values. Integer type  values..., -2, -1, 0, 1, 2,...  operations.
ISBN Chapter 7 Expressions and Assignment Statements.
MT311 Java Application Development and Programming Languages Li Tak Sing ( 李德成 )
1 CS Programming Languages Class 09 September 21, 2000.
March 31, ICE 1341 – Programming Languages (Lecture #11) In-Young Ko Programming Languages (ICE 1341) Lecture #11 Programming Languages (ICE 1341)
MT311 Java Application Development and Programming Languages Li Tak Sing ( 李德成 )
Copyright © Curt Hill The Assignment Operator and Statement The Most Common Statement you will use.
7-1/27 Chapter 7 Expressions and Assignment Statements Introduction Arithmetic Expressions Overloaded Operators Type Conversions Relational and Boolean.
Expressions and Assignment Statements
Expressions and Assignment Statements
7.2 Arithmetic Expressions
Data Types In Text: Chapter 6.
Chapter 7: Expressions and Assignment Statements
Type Checking Generalizes the concept of operands and operators to include subprograms and assignments Type checking is the activity of ensuring that the.
Type Checking, and Scopes
Expressions and Assignment Statements
Expressions and Assignment
Chapter 7: Expressions and Assignment Statements
Records Design Issues: 1. What is the form of references?
Expressions and Assignment Statements
Expressions and Assignment Statements
Arithmetic Expressions
Expressions and Assignment Statements
College of Computer Science and Engineering
Chapter 7 Expressions and Assignment Statements.
Compiler Construction
PRESENTED BY ADNAN M. UZAIR NOMAN
Compiler Construction
Presentation transcript:

MT311 Java Application Development and Programming Languages Li Tak Sing ( 李德成 )

Record Type Nearly all modern programming languages have ways to represent a number of data items as one collection, usually using some form of record types. The next reading discusses the design and implementation of record types.

Record Type The key questions to answer as you look at the design of record types are: – What is the syntax of referencing a field in a record? The use of the -> operators in C increase the writability. – Are elliptical references allowed? Elliptical references means omitting the record name in a reference to a record field when no ambiguity will result.

Record Type – What operations are allowed on the whole record? Apart from referencing fields, usually only a few operations are allowed on a record as a whole in typical programming languages. For example in C, the operations allowed are taking the address (the & operator) and assignments:

Record Type struct product { int prod_no; float price; float weight; } prod1, newprod; struct product *p;... newprod = prod1; p = &newprod;

Record Type – Is it possible to denote a value of a record type? Without this capability and if we want to change the values of all the fields of a record, we have to use a number of assignment statements to change the fields one by one. With this capability, we can just use one assignment statement to assign the denoted value to the record. For example, consider the following Ada code:

Record Type type rec_type is record A: String(1..20); B: Integer; end record; Now if we have a variable C of type rec_type, we use the following assignment statement to change the value of C: C:=(“REC”,4);

Record Type If this is to be done in Pascal, then two assignment statements are needed. Such denotation of record values is allowed in C only when a variable is initialized.

Union types A union is a type that allows a variable to store values of different data types at different times during program execution. It is one way of solving the problem of not allowing a variable to change type during execution. In addition, it allows us to define heterogeneous data structures such as tree structures of integers or floating points.

Union types However, the second use of union types is diminishing in object oriented languages because we can inherit different children from a root class and then construct a tree structure of the root class. Such structure should be capable of storing records of the children of different types. This method is more reliable than one that uses union types.

Union types When you think about union types in a programming language, try to find solutions to these questions: – Is dynamic (run-time) checking of data type required? Most languages do not check the type of the data stored in a union with respect to a tag. In this case there will be no assurance that the value stored is in the intended data type. This will make some data type errors undetected.

Union types – Is union a special construct inside a record, or is it a new data type in the language? In languages such as C, C++, union is a data type in itself. In Pascal and Ada, the union must be part of a record, since at least a tag is required to be stored alongside with the union.

Pointer types Allowing pointer types adds writability to a language. The programmer can more flexibly handle dynamic data structures like binary trees. Of course this flexibility may lead to problems like dangling pointers or lost heap- dynamic variables.

Pointer types The key point in the design of pointer types are: What is the lifetime of a pointer variable? Many pointer variables are storage bound at run time with memory allocated from the heap. They are called heap-dynamic variables.

Pointer types Are pointers restricted as to the data types of values to which they can point? For example, in C, most of the time (but not always) you have to predefine the type of value pointed to by a pointer, e.g.: int *ptr1; double *ptr2, *ptr3;

Pointer types Does the language allow pointer types, reference types or both? We can view reference type variables as a restricted but safer form of pointers, since pointer arithmetic is not allowed on them. They are included in languages such as C++ and Java.

Expression In a programming language variables with data types become one of the building blocks of expressions, and it is through expressions that we can perform calculations of various kinds. The other building blocks that compose an expression are constants, function calls, parentheses and operators.

Operator evaluation order We all know that * and / has a higher precedence than + and -. However, how about boolean operators, unary operators like ++, -- in C or Java? Boolean operators are likely to have lower precedence than + and - because the results of + and - can be operands of boolean operators but the reverse is not true.

Operator evaluation order For example: the expression 4+2=3*4 is first evaluated to: 6=7 and is then evaluated to false. If = has a higher precedence, then the expression would first evaluated to: 4+false*4 which is an invalid expression.

Associative, left-associative, and right-associative An operator  is associative if (a  b)  c =a  (b  c) we understand that + and * are associative. So - and / are not associative. An operator  is left associative if a  b  c = (a  b)  c So - and / are left associative. An operator  is right associative if

Associative, left-associative, and right-associative An operator  is right-associative if a  b  c = a  (b  c) exponentiation of real number is right- associative.

Operand evaluation order When an operator has two operands, then the result may be different if the order of the evaluation of the order is different. Consider the following example: int i=4; int j=(i++)+(i--);

Operand evaluation order If the left operand of the + operator is evaluation, the result is j=4+5=9 otherwise, the result is: j=3+4=7

Operand evaluation order This would only happen if the operand has side-effect, i.e., evaluating the operand will have changes to some of the variables. If all the operands have no side effect, then the order of evaluating the operands are not important

Overloaded operator The same operator can mean different operations according to the situation. * means multiplication of two numbers in C, but it also means dereferencing a pointer in C, if we use it as an unary operator. The concept that there are multiple meanings of an operator in different context is called operator overloading.

Overloaded operator Some languages like C++, Ada, etc even allow a user to define additional meaning to an operator — in this case we have user- defined operator overloading.

Type conversion Coercion is the implicit type conversion when an operator encounter operands of different types. For example: int a=2; float b=2.3; float c=a+b; In the example, a will be converted to float first before it is added.

Type conversion Too much coercion may defeat the type checking mechanism of the language because incompatible operands will be converted to compatible types without the note of the programmer. So reliability is reduced. Too less coercion will put the type conversion task on the shoulder of the programmer. So writability is decreased.

Explicit type conversion Explicit type conversion is required for a strong typed language because, from time to time, there is a need to use the value of a variable in an expression that is not type compatible with the variable. However, this kind of conversion may lead to problems. In C, you can convert a pointer of one type to point to a value of any type. For example, the following program is syntactically correct:

Explicit type conversion #include int main() { float a,b; int c, *t; b=1.0; t=(int*) &b; } t is pointing to a value that is supposed to be of type integer. However, the value is actually of type floating point.

Short-circuit evaluations The binary Boolean operators, AND and OR, seem to be simple and innocent enough. But on closer look we could find out that the Pascal and, or operators are very different from the Java &&, || operators, due to the short-circuit evaluation of the Java operators.

Short-circuit evaluations Consider the expression: A and B if A has been evaluated to false, then, it does not matter whether B is true or false, the result will always be false. So in short circuit evaluation, if the result of an expression is already determined regardless of the value of the remaining operand, then it will stop further evaluation and just return the result.

Short-circuit evaluations Similarly, if in the expression A or B and A has been evaluated to be true, then, in short- circuit evaluation, B will not be evaluated and true is returned. Then, what is the problem if short-circuit evaluation is used?

Short-circuit evaluation if in evaluating the operands of an expression, there are side effects and these side effects are what the programmer wants, then, using short-circuit evaluation may have problem because not all of these side effect may take place. Any way, as a good programmer, should not use side effects to achieve your purpose.

Short-circuit evaluation So what is the advantages of short-circuit evaluation? – it is more efficient in the evaluation of the expression as sometimes there is no need to evaluate the whole expression; – it solves the problem of evaluating a Boolean expression that is a conjunction of two so that when the first conjunct is false, the second conjunct is undefined.

Short-circuit evaluation – The following is an example: (i>0) and (log(i)>0) – if i is negative and the above expression is not short-circuit evaluated, then an error would occur because log(i) is undefined. However, if the expression is short-circuit evaluated, then the expression would have the value false. Thus writability is increased.

Assignments The assignment statement is another feature that started out simple in older languages but has evolved to many different forms in modern languages such as C, C++ and Java.

Assignments Multiple targets. Statements like a, b, c:=d; will not only increase the writability, it will also increase the efficiency. This is because after the value of d was assigned to c, this value should already be in a register of the CPU. Therefore, the value can be copied to the address of b directly.

Assignments If the above statement was separated into four, then the value has to be loaded into a register for four times. Assignment as expression. Again, this would enable the compiler to produce more efficient code because the assigned value is already in a register after the execution of an assignment statement. However, it may decrease the readability if an assignment is mixed with other expression.