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 Influences on language design There are two more factors that can affect the design of a language: – computer architecture – software development methodology.

3 Computer architecture Computer architecture may affect the design of a language. For example a 32-bit or a 64- bit processor may affect the size of an integer.

4 Software development methodologies There are mainly three kinds of software development methodologies: – Top-down design and stepwise refinement – Object-oriented design – Process-oriented design

5 Top-down design and stepwise refinement The main task is subdivided into smaller tasks. These smaller tasks are further subdivided until they are small enough to be implemented as a module or a subprogram. For example, the program for controlling an automatic teller machine can be subdivided into the following sub-tasks: password validation, balance checking, money delivery, updating account. Then these tasks may be further divided.

6 Object-oriented design The objects exist in the real world are modeled by encapsulating their attributes and operations. Then the whole system is modeled by the interactions between these objects. For example, one of the objects in the automatic teller machine system is the customer. The attributes of the customer are customer number, name, etc. An operation of a customer is money withdrawal.

7 Process-oriented design Different processes are modeled separately and the whole system is modeled by the interactions between these processes. This is mainly used in real time concurrent systems. For example, in a nuclear power plant system, there should be a process that regulates the water temperature and a process waiting to receive commands from a control panel. When the latter process receives a command to regulate the water temperature, it passes a signal to the former process to do the job.

8 The stepwise refinement methodology The stepwise refinement methodology promotes the use of subprograms and modules. For example, standard Pascal does not support modules and therefore is not very useful when this methodology is used.

9 The object-oriented methodology The object-oriented methodology promotes the use of object-oriented programming languages. Therefore, when this methodology becomes more popular, object- oriented programming languages would also gain in popularity. At this moment, you have at least learned one object oriented programming language, namely Java.

10 Process-oriented methodology The process-oriented programming methodology promotes the use of process- oriented programming languages like CSP or Ada.

11 Language categories Imperative languages. The procedures on how to perform the computation are stated explicitly, e.g., C, Pascal; Object-oriented languages. The behavior of different objects are stated explicitly, e.g., Smalltalk, Java;

12 Language categories Functional programming languages. The result, but not the procedures, of applying a function to some parameters are stated explicitly, e.g., LISP; Logic programming languages. The rules that have to be observed are stated explicitly, e.g., Prolog.

13 Variables, data types and expressions Variables have six attributes: – name – address – value – type – lifetime – scope.

14 Variable Name The maximum length of a name. Short names reduce readability. For example, item_number would be much self-explaining then itemno. The connector characters that are allowed to be used in a name. You can see that the use of ‘_’ in names would also increases readability. For example, item_number would be easier to read than itemnumber.

15 Variable Name The case sensitivity of names. Languages that are case sensitive have the potential of being misused. For example, if Edge and edge are two variables, then both the programmer and reader of the program may be confused.

16 Variable name Reserved words or keywords in the language. If a language has too many reserved words, then a program in this language is more difficult to write. This problem is particular severe with COBOL, which has more than 300 reserved words. It is a headache for a COBOL programmer to avoid using a reserved word as an identifier. On the other hand, PL/1 does not have reserved words. It only has keywords.

17 Variable name Therefore, you can write statements like this: IF IF=ELSE THEN THEN=ELSE; ELSE ELSE=END; END The disadvantage of no reserved words is that if a very common keyword, like IF, is given a new meaning, then the program would be very difficult to read.

18 Variable name There are two advantages of having no reserved words: – The programmer does not have the problem to use an identifier that has been reserved. – If reserved words are used and the language is extended, then there may be new reserved words. Then existing codes may no longer be valid.

19 Variable address, type and value Address is the location of the memory space for a variable. The time when this address is fixed is called the binding time. It can be fixed at load time (this occurs with variables in FORTRAN) or it can be fixed at run time (as for local variables of a sub-program in Pascal or C). A more detailed discussion of binding time is given in the next section of the unit.

20 Variable address, type and value Type is the range of possible values of a variable. Primitive data types are defined when the language is designed. User defined types are defined when a program is written.

21 Variable address, type and value Some languages do not check whether a value stored in a variable is within the defined range because doing so would greatly decrease the efficiency of the program. Most languages do not check whether a variable has been initialized before it is used. It is therefore the programmer’s responsibility to write extra codes to do this if he/she really wants such checking.

22 Lifetime and scope The remaining two attributes of variables are lifetime and scope. The lifetime of a variable begins when memory space is allocated to the variable and ends when the space becomes unavailable. The scope of a variable is the range of statements in which the variable can be referenced.

23 Lifetime and scope For example, in this C function: double area_of_circle(double radius) { double area; area = 3.1415 * radius * radius; return area; }

24 Lifetime and scope The lifetime of the variable area begins when the function is invoked (when storage is created for area ) and ends when the function returns its value to the caller (when the storage for area is to be returned to the system). The scope of area begins just after it is declared as a double and ends at the end of the function.

25 The concept of binding Binding is the abstract concept of the association between an attribute and an entity (e.g. the association of a type to a variable), or between an operation and a symbol (e.g. the association of the multiplication meaning to the symbol *). We call the time when the binding happens the binding time.

26 Binding Consider the following C statements: void myfunction(int a) { int x; ………… x=a+3; ………… otherfunction(x); }

27 Binding

28 Bindings can happen at any of these times: – Language design time. For example, the meaning of the operation ‘+’ is defined when the language is designed. – Language implementation time. The possible range of int is usually defined when the language is implemented. Some C compilers may define int to have the range –32768 to 32767, while others may define it differently.

29 Binding – Link time. For example, the relative address of the function otherfunction is bound when the program is linked. The address is relative because it can be loaded into anywhere in the memory when the program is executed. – Compile time. For example, the type of the variable x is defined when this program is compiled.

30 Binding – Load time. For example, the absolute address of the function otherfunction is bound when the program is loaded. – Run time. For example, the address of the variable x is bound when myfunction is called during execution. Note that if myfunction is a recursive function, then there can be multiple instances of x at one time.

31 Static binding or dynamic binding We can distinguish two types of bindings. A binding is called static if it occurs before run time and the binding is unchanged throughout program execution. On the other hand, a binding is dynamic if it occurs during run time or can change during program execution. When we look at the designs of languages, we can distinguish whether bindings are static or dynamic. Each binding type can be examined according to data type and storage.

32 Type binding To appreciate the designs of different programming languages and explore type bindings, let’s ask these questions: – How and when is the data type of a variable in a program decided? – Does the language allow the data type of a variable changed during program execution?

33 Static binding Many programming languages like C, Pascal, FORTRAN and COBOL have static type bindings: – the data type of a variable is defined using explicit declaration or implicit declaration, and the compiler takes note of the data type. Type binding occurs at compilation time; – the data type of a variable does not change throughout the program execution.

34 Static binding This method of binding has one advantage that the type of a variable is known at compile time and therefore the compiler can detect errors resulting from using incompatible types. For example, consider the following Pascal fragment:

35 Static binding var a:boolean; i:integer;... a:=i+1; The Pascal compiler would complain about this because an integer value is assigned to a Boolean variable. This is possible because the types of the variables are known during compile time and they cannot be changed in the run time.

36 Dynamic binding Some programming languages like APL, SNOBOL4 have dynamic type binding: – the data type of a variable is determined at run time (by the interpreter) — type binding occurs at run time; – the data type of a variable can change in the course of program execution.

37 Dynamic binding Dynamic type binding has one advantage: – It enables us to write generic subroutines, i.e. the same subroutine can be used for many different variables. For example, the same sorting subroutine can be used to sort different types of variable. On the other hand, if static type binding is used, then usually, a subroutine is only written for a particular type of variable.

38 Dynamic binding Dynamic type binding has two disadvantages: – he type checking mechanism of static type binding cannot operate; – the running cost is higher.

39 Storage bindings and the lifetime of a variable In order to see how the designs of programming languages can differ, let’s tackle these two questions: – How and when does the address of a variable get assigned? – How is the lifetime of a variable determined in the language?

40 Storage bindings and the lifetime of a variable The lifetime of a variable is the time during which the variable is bound to specific memory locations. It begins when memory locations are taken from a pool of available memory to be used by the variable — allocation. It ends when the memory locations previously bound to the variable are placed back to the pool of available memory — deallocation.

41 Storage bindings and the lifetime of a variable Four categories of variables can be distinguished according to their lifetime: – Static variables — they are bound to specific memory locations before program execution begins and remain bound to the same locations until program execution ends. In Java, all static attributes of a class are static variables. public class AClass { static int staticVar; }

42 Storage bindings and the lifetime of a variable Stack-dynamic variables — they are bound to storage at run time, when their declaration statements are elaborated. Elaboration of a variable declaration occurs when execution reaches the code that does the allocation of memory spaces and binding to the variable. The memory for stack-dynamic variables is allocated from the runtime stack.

43 Storage bindings and the lifetime of a variable In Java, local variables of primitive types are stack-dynamic, e.g. the local variable sum below. Storage is allocated when the execution reaches the declaration statement, and it is deallocated when execution reaches the end of the scope of the variable.

44 Storage bindings and the lifetime of a variable 1. class AClass { 2. public void aMethod(){ 3. double sum=0; 4. for (int i=0; i<10; i++){ 5. sum+=i; 6. } 7. } 8. }

45 Storage bindings and the lifetime of a variable In the above example, memory is allocated to sum when execution reaches line 3, and is deallocated when execution reaches line 6. Although the storage bindings for stack- dynamic variables are dynamic (the storage is bound and unbound in the course of program execution), their type bindings are still static (the data type is bound at compile time and is fixed during program execution).

46 Storage bindings and the lifetime of a variable Explicit heap-dynamic variables — their storage is allocated and deallocated by using system functions in a program. The memory is usually allocated from the heap. They cannot be referenced by simple variable names, and are referenced using pointer variables. In Java, object variables are explicit heap- dynamic variables. Their storage is allocated when the ‘new’ method is called for a class. In most other languages, pointers are explicit heap-dynamic variables.

47 Storage bindings and the lifetime of a variable Implicit heap-dynamic variables — the storage, type and value are bound at run- time only when they are assigned values. We have an example of this kind of variable in APL: PRICE ← 48.5

48 Storage bindings and the lifetime of a variable PRICE is only allocated storage when execution reaches the above assignment statement. The data type is also set to be real at the same time.

49 Binding


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

Similar presentations


Ads by Google