Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "MT311 Java Application Development and Programming Languages Li Tak Sing ( 李德成 )"— Presentation transcript:

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

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

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

4 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:

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

6 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:

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

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

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

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

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

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

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

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

15 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;

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

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

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

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

20 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

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

22 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--);

23 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

24 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

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

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

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

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

29 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:

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

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

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

33 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?

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

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

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

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

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

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


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

Similar presentations


Ads by Google