Slides 09 Programming Languages - UET CS - Talha Waheed - Data Types
Slides 09 Programming Languages - UET CS - Talha Waheed - Data Types
– Data Types
FORTRAN I (1956)
• Limited to INTEGER, REAL, arrays
• link-list , binary trees modeled with arrays
• Ada (1983)
• User create unique type for every category of variables in
problem space
• system enforce uniqueness of types (base for ADT)
Syntax / Declaration
Boolean
- Simplest of all types int flag = 0;
- - ALGOL 60 introduced it.
- Could be implemented as bits, but often as bytes. 00000001
- In C there is no Boolean type (But C++ has it)
- Advantage: readability, Dis-advantage: ??
Floating Point
- Model real numbers, as approximations
- (PI can’t be stored exactly)
- Even 0.1 in decimal can’t be represented by finite binary digits
- Scientific languages support 2+ float types
- fractions and exponents
- Storage on new computers in IEEE float point standard 754
- Usually exactly like hardware, software simulation is slow
- Some languages allow accuracy specs in code e.g. (Ada)
Design issues
1. Is it just special kind of array or primitive type
(with no array-style subscript operations) ?
2. Is length of objects static or dynamic?
- Somewhat primitive
- Operations: Assignment, comparison, catenation, substring
reference
- FORTRAN has intrinsic support for pattern matching
Ada
N := N1 & N2 (concatenation)
N(2..4) (substring reference)
C / C++
- Not primitive
- Use char arrays , library of functions that provide operations
SNOBOL4
- String manipulation language
- Primitive
- Many operations, including elaborate pattern matching
Java
- Primitive type of String class and StringBuffer class
String Length Options
1. Static
- FORTRAN 77, Ada, COBOL
- e.g. in FORTRAN 90 CHARACTER (LEN = 15) NAME;
3. Dynamic
- flexible but overhead of allocation/deallocation
- in SNOBOL4, PERL
Evaluation of strings
- Strings aid in writebility
Dynamic length
- need run-time descriptor
- allocation/deallocation is biggest implementation problem
Ordinal Types (User Defined)
Ordinal type: range of possible values can be associated with set of
positive integers
1. Enumeration Types
User enumerates all possible values, which are symbolic constants.
e.g. in C++
typedef enum {North = 1, East = 2, West = 3, South = 4} Directions;
Design Issue:
Should symbolic constant be allowed to be in more than one type
definition?
typedef enum {Right = 0, Left = 1} Directions;
typedef enum {Right = 1, Wrong = 2} Answer;
Examples
Pascal
- cannot reuse constants; can be used for array subscripts e.g.
Array[LEFT] , for variables, case selectors
e.g. in case RIGHT:
- NO input or output; compared in conditions.
Ada
- constants can be reused (overloaded literals)
- disambiguate with context or type_name (one of them)
- can be used as in Pascal; CAN be input and output
C/C++
- like Pascal, except they can be input and output as integers
Java
- does not include an enumeration type
Evaluation of enumeration
a. Enhances Readability
e.g. no need to code a color as a number,
use Colors as Enumerations
Color = 0; vs. Color = BLACK;
b. Enhances Reliability
e.g. compiler can check operations and ranges of
values
2. Sub-range Type
An ordered contiguous subsequence of an ordinal type
Design Issue
How can they be used?
Examples
Pascal
- Subrange types behave as their parent types
- can be used as for variables and array indices
e.g. type position = 0 .. MAXINT;
Ada
- Subtypes are not new types, just constrained existing types
(so they are already compatible);
can be used as in Pascal, plus in case constants
e.g.
subtype POS_TYPE is
INTEGER range 0 ..INTEGER'LAST;
Evaluation of sub-range types
- Enhances Readability
Design Issues
Syntax
- FORTRAN, PL/I, Ada uses parentheses () name(1)
1. Static
- range of subscripts and storage bindings are static
e.g. FORTRAN 77, some arrays in Ada
Advantage: flexibility
- size need not be known until array is about to be used
4. Heap-dynamic
- subscript range and storage bindings are dynamic and not fixed
- Others - no limit
Array Initialization
list of values that are put in array in order in which array elements are
stored in memory.
Examples
1. FORTRAN - uses DATA statement,
or put values in / ... / on the declaration time.
1. Ada
- Assignment; RHS can be an aggregate constant or array name
2. FORTRAN 90
Examples
1. FORTRAN 90
INTEGER MAT (1 : 4, 1 : 4)
MAT(1 : 4, 1) - the first column
MAT(2, 1 : 4) - the second row
- Subscript Checking
Associative Arrays (Maps)
Associative array: unordered collection of data elements, indexed by equal
number of values called keys. (Key, Value pair).
Something like 2 d array, one have data other have keys for indexing.
Design Issues
Design Issues:
2. Initialization
- Allowed in Ada, using aggregate constant
3. Comparison
- In Ada, = and /=; one operand can be aggregate constant
4. MOVE CORRESPONDING
- In COBOL - it moves all fields in source record to
fields
with same names in destination record
Comparing records and arrays
1. Access to array elements is much slower than access to
record fields, because subscripts are dynamic (field
names are static)
2. Dynamic subscripts could be used with record field
access, but it would disallow type checking and it would be
much slower
Unions
Design Issues:
b. tag is optional! Now, only declaration and second and last assignments are
Union – Examples from Different Languages
4. Ada - discriminated unions
- Reasons they are safer than Pascal & Modula-2:
a. Tag must be present
b. It is impossible for user to create inconsistent union
(because tag cannot be assigned by itself,
all assignments to union must include tag value)
Evaluation
- If language does not have sets, they must be simulated,
either with enumerated types or arrays
- Arrays more flexible than sets, but much slower operations
Implementation
- Usually stored as bit strings and use logical operations for
set operations
Set Examples from Different Languages
1. Pascal
- No maximum size in language definition
- Operations: union (+), intersection (*), difference (-),
equal (=), not equal (<>), superset (>=), subset (<=), in