PL-5 Handout
PL-5 Handout
PL-5 Handout
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.
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.
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