PL-5 Handout

Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

IT2009

Data Types
Primitive Data Types
 A data type defines a collection of data values and a set of predefined operations on those values.
 Data types that are not defined in terms of other types are called primitive data types.
 The primitive data types of most imperative languages include numeric, Boolean, and character types.
 Numeric types include:
o Integer is the most common primitive numeric data type
o Floating-point data types model real numbers, but the representations are only
approximations of many real numbers.
o Decimal data types store a fixed number of decimal digits, with the decimal point at a fixed
position in value.
 Boolean data type has range of values that has only two (2) elements, one for true and one for false.
 The character data type is used to store only a single character. Examples of characters include letters,
numerical digits, common punctuation marks, and whitespace.

Character String Types


 A character string (or string) type has values that consist of sequences of characters.
 The most common string operations are: assignment, concatenation, substring reference,
comparison, and pattern matching.
 Three (3) string length options:
o Static length string: The length of string values can be fixed and set when the string is created.
o Limited dynamic length string: The length of string values can have varying length up to a
declared and fixed maximum set by the variable’s definition.
o Dynamic length string: The length of string values can have varying length with no maximum.

User-Defined Ordinal Types


 An ordinal type consists of a range of possible values that can be easily associated with the set of
positive integers.
 Two (2) user-defined ordinal types:
o An enumeration type is one in which all of the possible values, which are named constants,
are provided, or enumerated, in the definition.
o A subrange type represents a subset of the values in another ordinal type.

Structured Data Types


 An array is a container object that stores a fixed number of values of a single type.
 The individual elements of an array are referenced using a subscript or index.
 Five (5) categories of arrays based on the binding to subscript ranges, the binding to storage, and from
where the storage is allocated:
o A static array has subscript ranges that are statically bound and its storage allocation is static
(done before run time).
o A fixed stack-dynamic array has subscript ranges that are statically bound, but its allocation
is done at declaration elaboration time during execution.
o A stack-dynamic array has subscript ranges and storage allocation that are both dynamically
bound at elaboration time.
o A fixed-heap dynamic array is similar to a fixed-stack dynamic array, but the subscript ranges
and the storage binding are both fixed after storage is allocated.
o A heap-dynamic array has subscript ranges and storage allocation that are both dynamically
bound and can change during the array’s lifetime multiple times.
 The most common array operations are assignment, concatenation, comparison for equality and
inequality, and slices.

05 Handout 1 *Property of STI


Page 1 of 2
IT2009

 A rectangular array is a multidimensional array in which all of the rows and columns have the same
number of elements.
 A jagged array consists of rows with different number of elements.
 After extracting certain elements from an array, another array is produced called a slice.
 An associative array is an unordered collection of data elements that are indexed by an equal number
of values called keys.
 A record is a collection of data elements in which the individual elements are identified by names and
accessed through offsets from the beginning of the structure.
 A tuple is a data type that is similar to a record, except that the elements are not named.
 A list represents are ordered sequence of values, which is usually not separated by any punctuation.
 A union is a type whose variables may store different type of values at different times during program
execution.
o Unions in programs that are free from type checking are called free unions.
o Type checking of unions requires that each union construct include a type indicator. Such
indicator is called a tag, or discriminant, and a union with a discriminant is called a
discriminated union.

Pointer and Reference Types


 A pointer type is one in which the variables have a range of values that consists of memory addresses
and a special value called nil.
 The nil value is not a valid address and is used to indicate that a pointer cannot currently be used to
reference a memory cell.
 A pointer can be used to access a location in an area where storage is dynamically allocated called a
heap.
 Variables that are dynamically allocated form the heap are called heap-dynamic variables.
 Variables without names are called anonymous variables.
 Two (2) fundamental pointer operations:
o Assignment sets a pointer variable’s value to some useful address.
o Dereferencing takes a reference through one (1) level of indirection.
 A reference type variable refers to an object or a value is memory.

Other Concepts
 Type checking is the activity of ensuring that the operands of an operator are of compatible types.
 The process automatically converting an operator is called a coercion.
 A type error is the application of an operator to an operand of an inappropriate type.
 Type checking at run time is called dynamic type checking.
 A programming language is strongly typed if type errors are always detected.
 Type equivalence means that two (2) types are equivalent if an operand of one (1) type in an
expression is substituted for one of the other type, without coercion.
 The two (2) approaches to defining type equivalence are name type equivalence and structure type
equivalence.

References:

Sebesta, Robert W. (2012). Concepts of Programming Languages. 10th ed. USA: Pearson Education, Inc.

Ben-Ari, Mordechai (2006). Understanding Programming Languages. Chichester: John Wiley & Sons, Inc.

Tucker, Allan B. and Noonan, Robert E. (2002). Programming Languages: Principles and Paradigms. 2nd ed.
New York: Mc-Graw Hill

05 Handout 1 *Property of STI


Page 2 of 2

You might also like