1 cosc3306: programming paradigms lecture 2: data types haibin zhu, ph.d. computer science nipissing...

48
1 COSC3306: COSC3306: Programming Programming Paradigms Paradigms Lecture 2: Data Types Lecture 2: Data Types Haibin Zhu, Ph.D. Haibin Zhu, Ph.D. Computer Science Computer Science Nipissing University Nipissing University (C) 2003 (C) 2003

Upload: della-george

Post on 02-Jan-2016

214 views

Category:

Documents


0 download

TRANSCRIPT

11

COSC3306:COSC3306:Programming ParadigmsProgramming Paradigms

Lecture 2: Data TypesLecture 2: Data Types

Haibin Zhu, Ph.D.Haibin Zhu, Ph.D.Computer ScienceComputer ScienceNipissing University Nipissing University

(C) 2003(C) 2003

22

Data ObjectData Object

A A data objectdata object, also known as a , also known as a variablevariable or or identifieridentifier, , can be characterized by four components: L, N, V, and can be characterized by four components: L, N, V, and T.T.– L is the L is the locationlocation and holds the value of a data object. The action and holds the value of a data object. The action

that allocates a location for a data object is called that allocates a location for a data object is called allocationallocation. An . An allocation performed before execution is called static allocation; allocation performed before execution is called static allocation; and if performed at run time it is called and if performed at run time it is called dynamic allocationdynamic allocation..

– N is the N is the namename of an identifies and is used to refer to the data of an identifies and is used to refer to the data object.object.

– V is the V is the valuevalue of data object and is represented in coded form in of data object and is represented in coded form in the allocated location of the data object.the allocated location of the data object.

– T is the data T is the data typetype and represents the possible form of values and represents the possible form of values that a data object can hold.that a data object can hold.

33

Data Object (cnt’d)Data Object (cnt’d)

A A local variablelocal variable is a variable that is is a variable that is declared within a block for use only within declared within a block for use only within that block.that block.

A A global variableglobal variable is a variable that is is a variable that is declared in the outermost block of the declared in the outermost block of the program and can be used anywhere in a program and can be used anywhere in a program. program.

44

Data TypeData Type

In any programming language data and In any programming language data and operations are related through a operations are related through a mechanism known as a mechanism known as a data typedata type, which , which usually has a set of operations that can be usually has a set of operations that can be applied to values. applied to values.

55

Design and Implementation:Design and Implementation:

For each data type four important For each data type four important elements must be considered:elements must be considered:– Set of valuesSet of values

– Set of operationsSet of operations

– The internal representationThe internal representation

– The external representationThe external representation

66

Type InformationType Information

From the declaration point of view type From the declaration point of view type information can be categorized asinformation can be categorized as– Implicit type informationImplicit type information– Explicit type information.Explicit type information.

77

Implicit type informationImplicit type information

Implicit type informationImplicit type information includes types of includes types of constants and types of variables that may not be constants and types of variables that may not be given in a declaration. In this case the type must given in a declaration. In this case the type must be inferred by the translator, either from context be inferred by the translator, either from context information or from standard rules. information or from standard rules.

For example, 10 is implicitly an integer, true is a For example, 10 is implicitly an integer, true is a Boolean, I is an integer data object in the Boolean, I is an integer data object in the FORTRAN programming language, 2.5 is a FORTRAN programming language, 2.5 is a double in C++.double in C++.

88

Explicit type informationExplicit type information

Explicit type informationExplicit type information is contained in is contained in declarations. Variables can be declared to declarations. Variables can be declared to be a specific data type, such asbe a specific data type, such as

intint x;x;

floatfloat y;y;

where x and y are two variables of integer where x and y are two variables of integer and real types, respectively.and real types, respectively.

99

User’s point of view: Type User’s point of view: Type information classificationinformation classification

Built-in data typesBuilt-in data types, which are included , which are included within the language definition and can be within the language definition and can be used through the declaration of the data used through the declaration of the data objectsobjects

User-defined data typesUser-defined data types, which are , which are defined through a declaration of the type defined through a declaration of the type before its use in a data object declaration. before its use in a data object declaration.

1010

Data Type Categories in Data Type Categories in Programming LanguagesProgramming Languages

Primitive data typesPrimitive data types

Composite data typesComposite data types

Recursive data typesRecursive data types

1111

Primitive data typesPrimitive data types

Primitive data typesPrimitive data types, sometimes called , sometimes called unstructured data typesunstructured data types, have elements , have elements consisting of indivisible entities and consisting of indivisible entities and therefore cannot be decomposed into therefore cannot be decomposed into simple.simple.– Integer TypeInteger Type– Real TypeReal Type– Boolean TypeBoolean Type– Character TypeCharacter Type

1212

Integer TypeInteger Type

Most languages are Most languages are implementation implementation dependent.dependent.– -32768----32767 (2 -32768----32767 (2

byte integer)byte integer)– -2-23131------2------231 31 -1 (4 byte -1 (4 byte

integer)integer)

Java integer is Java integer is definite. definite.

typetype Size Size (bits)(bits)

bytebyte 88

short short 1616

intint 3232

longlong 6464

1313

Real TypeReal Type

Floating-point numbers used to store data containing a Floating-point numbers used to store data containing a decimal point and a fractional part. (Language decimal point and a fractional part. (Language dependent)dependent)– 2.345, -15.0, 1.56e2, 1.56e-2, …2.345, -15.0, 1.56e2, 1.56e-2, …– Eg: s, exp, f1, f2 (s0.1f1f2*2Eg: s, exp, f1, f2 (s0.1f1f2*2(exp)(exp)))

ANSI C:ANSI C:– float, double, long doublefloat, double, long double

JavaJava– The "float" class takes 4 bytes of storage, and have 23 binary The "float" class takes 4 bytes of storage, and have 23 binary

digits of precision. The "double" class takes 8 bytes of storage, digits of precision. The "double" class takes 8 bytes of storage, and have 52 binary digits of precision. and have 52 binary digits of precision.

– 3.0d is a double precision floating-point number constant. 3.0, or 3.0d is a double precision floating-point number constant. 3.0, or alternatively 3.0f is a single precision floating-point number alternatively 3.0f is a single precision floating-point number constant. constant.

1414

Boolean TypeBoolean Type

One bitOne bit

True (1) / false (0)True (1) / false (0)

NOT, AND, ORNOT, AND, OR

1515

Character TypeCharacter Type

Expresses a single characterExpresses a single character

Internal expression: ASCII code (7/8 bits)Internal expression: ASCII code (7/8 bits)– http://www.cplusplus.com/doc/papers/ascii.hthttp://www.cplusplus.com/doc/papers/ascii.ht

mlml

Java:Java:– ISO Unicode (16 bits, 65536=2ISO Unicode (16 bits, 65536=21616))

1616

Composite Data TypesComposite Data Types

Composite data typesComposite data types, sometimes called , sometimes called structured data typesstructured data types, are compound types , are compound types such as arrays, records, tuples, lists, sets, such as arrays, records, tuples, lists, sets, functions, strings, pointers, linked lists, functions, strings, pointers, linked lists, stacks, queues, and serial and direct files.stacks, queues, and serial and direct files.

1717

Composite data types classificationComposite data types classification

Composite data types are classified:Composite data types are classified:– HeterogeneousHeterogeneous, whose elements are of , whose elements are of

different types (e.g., records and structures)different types (e.g., records and structures)– HomogeneousHomogeneous, whose elements are of the , whose elements are of the

same type (e.g., arrays).same type (e.g., arrays).

1818

ArrayArray

A collection of two or more adjacent memory A collection of two or more adjacent memory cells, called array elements.cells, called array elements.In C++:In C++:– int arr[ ]= {1,2,3,4,5};int arr[ ]= {1,2,3,4,5};– char str[] = new char [20];char str[] = new char [20];– char *str = new char [20];char *str = new char [20];– char str[20];char str[20];

In JavaIn Java– int [] arr = new int [5];int [] arr = new int [5];– char [] charArray = {‘a’, ‘e’, ‘i’, ‘o’, ‘u’}; char [] charArray = {‘a’, ‘e’, ‘i’, ‘o’, ‘u’}; – String [] TAs = {“Kelly Brown”, “Andy Shulte”, “Brent String [] TAs = {“Kelly Brown”, “Andy Shulte”, “Brent

Haas”};Haas”};

1919

StringString

The values are consist of sequences of The values are consist of sequences of characters.characters.In C++:In C++:– char s[20];char s[20];– string s; //STLstring s; //STL

In Java:In Java:– String s = “good”; String s = “good”; – String s;String s;– s = “good”;s = “good”;

2020

EnumerationEnumeration

List all the values that can be taken.List all the values that can be taken.

In C++: In C++: – enum weekday = {Sun, Mon, Tue, Wed, Thu, enum weekday = {Sun, Mon, Tue, Wed, Thu,

Fri, Sat}Fri, Sat}

Internal expression: Internal expression: – several bits or one bit one value to get high several bits or one bit one value to get high

speed.speed.

2121

PointerPointer

Contains the address of another variable.Contains the address of another variable.– int r, *p, q;int r, *p, q;– r =5;r =5;– p = &r;p = &r;– q = *p;q = *p;

© 2003 Brooks/Cole Publishing / Thomson Learning™

2222

StructureStructure

Constructed by using objects of different data Constructed by using objects of different data types.types.In C:In C:– struct coursestruct course– {{– int number;int number;– char grade;char grade;– }}– struct course prog;struct course prog;– prog.number = 234;prog.number = 234;– prog.grade =‘A’;prog.grade =‘A’;

2323

FunctionFunction

A function is actually a struct in C.A function is actually a struct in C.– struct function {struct function {– string function_name;string function_name;– string returntype;string returntype;– string parameters[];string parameters[];– string declarations[];string declarations[];– string statements[];string statements[];– }}

Function pointersFunction pointers– you can transfer a function name to a function pointer you can transfer a function name to a function pointer

as a parameter of another function.as a parameter of another function.

2424

ListList

An ordered sequence of objects, in the An ordered sequence of objects, in the form of (x :: y), x is the 1form of (x :: y), x is the 1stst element, y is the element, y is the tail (a list)tail (a list)

Simple lists:Simple lists:– [1,2,3,4][1,2,3,4]– [“ab”, “cd”, “ef”][“ab”, “cd”, “ef”]– (cons 'pine '(fir oak maple)) (cons 'pine '(fir oak maple)) – =(pine fir oak maple) =(pine fir oak maple)

2525

Linked ListLinked List

struct node {struct node { int number; int number; char grade;char grade; struct node * next; struct node * next; }}

© 2003 Brooks/Cole Publishing / Thomson Learning™

2626

To be continuedTo be continued

Composite typesComposite types– StacksStacks– QueuesQueues

Recursive typesRecursive types

Type bindingType binding

Type checkingType checking

Type conversionType conversion

2727

StacksStacks

A A stackstack is a linear data structure that can be accessed is a linear data structure that can be accessed only from the top for storing and retrieving data.only from the top for storing and retrieving data.Design and Implementation:Design and Implementation: The stack is called a last- The stack is called a last-in first-out (LIFO) data structure. A stack is defined in in first-out (LIFO) data structure. A stack is defined in terms of operations that change its status and operations terms of operations that change its status and operations that check this status. The operations are as follows.that check this status. The operations are as follows.– Clear():Clear(): Clear the stackClear the stack– IsEmpty():IsEmpty(): Check to see if the stack in emptyCheck to see if the stack in empty– IsFull()IsFull() :: Check to see if the stack is fullCheck to see if the stack is full– Push(element):Push(element): Put the item Put the item elementelement on top of the stack on top of the stack– Pop():Pop(): Remove the element from the top of the stackRemove the element from the top of the stack

2828

Figure 2.3 Push and Pop operations performed on a stack

© 2003 Brooks/Cole Publishing / Thomson Learning™

2929

Stack for a recursive functionStack for a recursive function int f(int i)int f(int i) {{ if (i =0) return 0;if (i =0) return 0; else i+f(i-1);else i+f(i-1);}} f(5)f(5)

545

345

2345

12345

012345

012345

12345

3345

645

105 15

Push; …. … Pop, pop, add, push; …. …

3030

QueuesQueuesA A queuequeue is a linear data structure that grows by is a linear data structure that grows by adding elements to its top and shrinks by taking adding elements to its top and shrinks by taking elements from its bottom.elements from its bottom.Design and Implementation:Design and Implementation: The queue is The queue is called a first-in first-out (FIFO) structure, and called a first-in first-out (FIFO) structure, and operations are similar to stack operations. The operations are similar to stack operations. The operations are as follows.operations are as follows.– Clear():Clear(): Clear the queueClear the queue– IsEmpty():IsEmpty(): Check to see if the queue in emptyCheck to see if the queue in empty– IsFull()IsFull() :: Check to see if the queue is fullCheck to see if the queue is full– Enqueue(element):Enqueue(element): Put the item Put the item elementelement on top of the on top of the

queuequeue– Dequeue():Dequeue(): Remove the element from the bottom of Remove the element from the bottom of

the queuethe queue

3131

Figure 2.4 Enqueue and Dequeue operations performed on a queue

© 2003 Brooks/Cole Publishing / Thomson Learning™

3232

Recursive Data TypesRecursive Data Types

Sometimes referred to as Sometimes referred to as circular data circular data typestypes, have values that are composed , have values that are composed from values of the same type.from values of the same type.In C, we have In C, we have struct Node struct Node {{

int data;int data;Node * ptr;Node * ptr;

}}

In Pascal:

type link = ^cell; type cell = record

info: integer; next: link;

end;

3333

Type BindingType Binding

The determination of one of the components of a The determination of one of the components of a data object is called data object is called bindingbinding..Here is an example of a declaration in C Here is an example of a declaration in C programming language:programming language:

int A;int A;The declaration is interpreted as:The declaration is interpreted as:– A location able to hold an integer number is created A location able to hold an integer number is created

and a reference to it is given the name A.and a reference to it is given the name A.

In this example, the identifier A is bound to some In this example, the identifier A is bound to some locations (known to compiler, but not to the locations (known to compiler, but not to the programmer) and to the type integer.programmer) and to the type integer.

3434

A =B+CA =B+C

A binding between names and locations must be A binding between names and locations must be performed to obtain the addresses of A,B and C.performed to obtain the addresses of A,B and C.

A binding between locations and values must be A binding between locations and values must be performed to retrieve the data from the performed to retrieve the data from the addresses of B and Caddresses of B and C

Compute the value of expression B+CCompute the value of expression B+C

A new binding between the location and its A new binding between the location and its computed value must be established to store the computed value must be established to store the resulting value in the address of Aresulting value in the address of A

3535

Figure 2.5 A data object and its bindings

© 2003 Brooks/Cole Publishing / Thomson Learning™

3636

Type BindingType Binding

Binding can occur at three specific times, which Binding can occur at three specific times, which give them their names.give them their names.– Compile time bindingCompile time binding, also known as , also known as static bindingstatic binding, ,

sometimes referred to as sometimes referred to as early bindingsearly bindings, occurs when , occurs when the program is being translated into machine the program is being translated into machine language.language.

– Load time bindingLoad time binding occurs when the machine code occurs when the machine code generated by the compiler is being stored to memory generated by the compiler is being stored to memory locations; location binding occurs at load time. locations; location binding occurs at load time.

– Run time bindingRun time binding, also known as , also known as dynamic bindingdynamic binding, , sometimes referred to as sometimes referred to as late bindingslate bindings, occurs when , occurs when the program is being executed.the program is being executed.

3737

Symbol TableSymbol Table

Mathematically speaking, the symbol table is a function from names to attributes:

Symbol Table : Names => Attributes

It is a data structure used to store information about the source language constructs.

3838

Figure 2.6 Binding of attributes to names

© 2003 Brooks/Cole Publishing / Thomson Learning™

#include <stdio.h> main(){ int x, *p; x = 10}

Symbol tableName type address value … … main function 0x--- ----- x int 0x--- 0->10 p pointer 0x--- 0

Environmentmemory

3939

Figure 2.7 Symbol table maintained by a compiler

© 2003 Brooks/Cole Publishing / Thomson Learning™

4040

Figure 2.8 Symbol Table maintained by an interpreter

© 2003 Brooks/Cole Publishing / Thomson Learning™

4141

Type CheckingType Checking

Type checkingType checking is the process a translator goes through is the process a translator goes through to verify that all constructs in a program are valid in to verify that all constructs in a program are valid in terms of the types of its constants, variables, functions terms of the types of its constants, variables, functions and other entities.and other entities.For example, in the following statements in C language:For example, in the following statements in C language:

Z Z X X 5 5 Y; Y;Switch (XSwitch (X2, 2.5, Y);2, 2.5, Y);

Y must be of a type that permits multiplication by an Y must be of a type that permits multiplication by an integer, (5integer, (5Y). Similarly, the types of the actual Y). Similarly, the types of the actual parameters for the call to function Switch must be parameters for the call to function Switch must be checked for compatibility with the types of the formal checked for compatibility with the types of the formal parameters.parameters.

4242

Type Checking (cnt’d)Type Checking (cnt’d)

Type checking can occur at compile time, at run time, or Type checking can occur at compile time, at run time, or not at all. not at all. Compile time checking:Compile time checking: Indicating that type checking Indicating that type checking must be performed by the compiler with regard to the must be performed by the compiler with regard to the declaration of data objects. It can be viewed as declaration of data objects. It can be viewed as static static type checkingtype checking, in which type information is maintained , in which type information is maintained and checked at translation time. (C++)and checked at translation time. (C++)Run time checking:Run time checking: Indicating that type checking must Indicating that type checking must be performed every time a data object is accessed. It be performed every time a data object is accessed. It can be viewed as can be viewed as dynamic type checkingdynamic type checking in which type in which type information is maintained and checked at execution time. information is maintained and checked at execution time. (Smalltalk)(Smalltalk)

4343

Type ConversionType Conversion

Type conversionType conversion deals with two different data deals with two different data types in the evaluation of an expression. For types in the evaluation of an expression. For example, in the assignment statementexample, in the assignment statement

A A B B C; C;

the right operand of the assignment operator “the right operand of the assignment operator “” ” is the expression B is the expression B C that assigns a value to C that assigns a value to the left operand A as the result of the execution.the left operand A as the result of the execution.Converting data types may be done either Converting data types may be done either implicitly or explicitly. implicitly or explicitly. Conversion needs internal mechanisms to Conversion needs internal mechanisms to support.support.

4444

Implicit Data Type ConversionImplicit Data Type Conversion

Programming languages that allow implicit data type Programming languages that allow implicit data type conversions provide a list of all pairs of types for which conversions provide a list of all pairs of types for which data type conversion is permitted.data type conversion is permitted.For example, in Pascal the assignment statement x:=y is For example, in Pascal the assignment statement x:=y is legal when y is of type legal when y is of type intint and x is of type and x is of type realreal, indicating , indicating that the integer value y is converted to a real value to be that the integer value y is converted to a real value to be stored in x. stored in x. x= y is valid in C++, too.x= y is valid in C++, too. int x;int x; float y = 1.25;float y = 1.25; x = y ;x = y ;

4545

Mixed TypesMixed Types

In C, mixed types are permitted. For instance, the In C, mixed types are permitted. For instance, the assignment statementassignment statementxxyy2.5;2.5;where x and y are type where x and y are type intint, can be performed with , can be performed with conversion types. In this case, y is converted to a conversion types. In this case, y is converted to a realreal, , and the real value of yand the real value of y2.5 is truncated to an integer and 2.5 is truncated to an integer and can be assigned to x. can be assigned to x. Consider the following statements in Java language.Consider the following statements in Java language.

bytebyte A, B, C;A, B, C;A A B B C; C;

In this case, the values of B and C are coerced to In this case, the values of B and C are coerced to intint and and an integer addition is performed. Then the sum (Ban integer addition is performed. Then the sum (BC) is C) is converted to converted to bytebyte and stored in variable A. and stored in variable A.

4646

Explicit Data Type ConversionExplicit Data Type Conversion

When specific functions are called to When specific functions are called to perform the conversion from one type to perform the conversion from one type to another, the data type conversion is another, the data type conversion is explicit.explicit.

For example, in Ada the function FLOAT For example, in Ada the function FLOAT can convert any type to an equivalent typecan convert any type to an equivalent type floatfloat. Such conversions are normally . Such conversions are normally permitted between numeric data types.permitted between numeric data types.

4747

ExamplesExamples

Functions Functions trunctrunc and and roundround in Pascal and TRUNC and in Pascal and TRUNC and FLOAT in Modula-2. Another kind of explicit type FLOAT in Modula-2. Another kind of explicit type conversion is with a conversion is with a castcast, meaning that a value or an , meaning that a value or an object of one type can be converted to an equivalent object of one type can be converted to an equivalent value of another type.value of another type.For example, the C fragmentFor example, the C fragment intint a;a;

floatfloat bb12.5;12.5;a a (int) b;// same as a = int(b) in C++; (int) b;// same as a = int(b) in C++;

truncates the real value 12.5 to 12, thus performing an truncates the real value 12.5 to 12, thus performing an actual conversion with 12 as the value of the variable actual conversion with 12 as the value of the variable aa..

4848

SummarySummary

Data object (4 components)Data object (4 components)– L,N,V,TL,N,V,T

TypeType– Set of valuesSet of values– Set of operationsSet of operations– The internal representationThe internal representation– The external representationThe external representationType Checking (2 kinds)Type Checking (2 kinds)– Compile time and run timeCompile time and run time

Type conversion (2 kinds)Type conversion (2 kinds)– Implicit and explicitImplicit and explicit