Data Types
Data Types
TYPES
INTRODUCTION 6.1
Data types : A collection of data values and a set of predefined operations
Discriptor: Collection of attributes of a variable and can be a difference between Static and Dynamic
attributes.
• Numeric Data Type “Numeric Primitive”: (short, int, long, float, double)
Complex (array, map, struc, nested): these nested data structures composed of
primitive data types or other complex types
6.2
Example:
• Struct
• Array
6.2
• Boolean Types: A range value that only has two functions, true or false.
USER-DEFINED ORDINAL
TYPES
An ordinal type is one in which the range of possible values can be easily
associated with the set of positive integers
ENUMERATION TYPE
An enumeration type is one in which all of the possible values, which are
named constants, are provided, or enumerated, in the definition.
Enumeration types provide a way of defining and grouping collections of
named constants, which are called enumeration constants
Example:
enum days {Mon, Tue, Wed, Thu, Fri, Sat, Sun};
6.4
THE DESIGN ISSUES
Coercion of Enumeration Values to Integers
DESIGNS
6.4
In languages that do not have enumeration types, programmers usually simulate them
with integer values.
Example:
int red = 0;
int blue = 1;
6.4
SOLUTION
Example:
type
MyRange = 20..50;
ADA’s DESIGN 6.5
In Ada, subranges are included in the category of types called subtypes
Example:
type Days is (Mon, Tue, Wed, Thu, Fri, Sat, Sun);
Example:
subtype Weekdays is Days range Mon..Fri;
Example:
Day1 : Days;
Day2 : Weekdays;
Day2 := Day1;
6.5
EXAMPLE:
6.5
Fixed Stack-Dynamic Array
Stack-Dynamic Array
· both the subscript ranges and the storage allocation
are dynamically bound at elaboration time.
6.5
Fixed Heap-Dynamic Array
· similar to a fixed stack-dynamic array, in that the
subscript ranges and the storage binding are both
fixed after storage is allocated.
Heap-Dynamic Array
· subscript ranges and storage allocation is dynamic
and can change any number of times during the
array’s lifetime.
6.5
RECTANGULAR AND JAGGED
ARRAYS
• Rectangular array is a multi-dimensioned array in which all of the
rows have the same number of elements and all of the columns have
the same number of elements.
6.5
Jagged Array is one in which the lengths of the rows
need not be the same.
6.5
SLICES
Slice of an array is some substructure of that array.
6.5
IMPLEMENTATION OF
ARRAY TYPES
Implementing arrays requires considerably more compile-time
effort than does implementing primitive types.
6.6
ASSOCIATIVE ARRAYS
Associative array is an unordered collection of data elements that are
indexed by an equal number of values called keys.
In the case of non-associative arrays, the indices never need to be stored
(because of their regularity).
6.6
STRUCTURE AND OPERATIONS
In Perl, associative arrays are called hashes, because in the
implementation their elements are stored and retrieved with hash
functions.
The namespace for Perl hashes is distinct: Every hash variable name must
begin with a percent sign (%).
Records have been part of all of the most popular programming languages, except pre-90
versions of Fortran, since the early 1960s, when they were introduced by COBOL.
Definition: The fundamental difference between a record and an array is that recordele-
ments, or fields, are not referenced by indices. Instead, the fields are named with
identifiers, and references to the fields are made using these identifiers.
6.7
Example: 01 EMPLOYEE-RECORD.
02 EMPLOYEE-NAME.
05 FIRST PICTURE IS X(20). 05 MIDDLE PICTURE IS X(10).
05 LAST PICTURE IS X(20).
02 HOURLY-RATE PICTURE IS 99V99.
Evaluation:
Records are frequently valuable data types in programming languages. The Design of record types is
straightforward, and their use is safe.
The fields of records are stored in adjacent memory locations. But because the sizes of the fields are not
necessarily the same, the access method used for arrays is not used for records. I
6.8
TUPLE TYPES
A tuple is a data type that is similar to a record, except that the elements are not named
Tuples are used in Python, ML, and F# to allow functions to returnmul-tiple values.
tuples are closely related to its lists, except that tuples are immutable. A tuple is created
by assigning a tuple literal, as in the following example
6.8
Example:
The fundamental list operations in Scheme are two functions that take listsapart
and two that build lists. The CAR function returns the first element of itslist
parameter.
Example 1:
A list of 10 natural numbers.
6.9
1 2 3 4 5 6 7 8 9 10
0123456789
Example 2:
A list with different types of items
A list comprehension is an idea derived from set notation. It first appeared in the functional
programming language Haskell
The mechanics of a list comprehension is that a function is applied to each of the elements of a
given array and a new array is constructed from results
6.10
UNION TYPES
FREE UNIONS
In some programming languages like C and C++, unions are often
called "free unions" because there's no built-in support for
checking the type of data stored in them.
6.10
DISCRIMINATED UNIONS
Each union has a special indicator called a tag or discriminant,
which tells the program what type of data is currently stored in the
union.
6.10
6.11
POINTER AND REFERENCE
TYPES
Pointer Type : variables have range of values that consist of memory addresses and special value
Reference type: variable similar to a pointer, with one fundamental difference, pointers refers to addresses
while reference refers to the object of value in memory
Heap management:
• Single size Cells
• Reference counters
• Counter mark-sweep
• Eager approach & Lazy approach
6.12
TYPE CHECKING
Type checking is the activity of
ensuring that the operands of an
operator are of compatible
types. A compatible type is one
that either works directly with
an operator or can be
automatically changed by the
computer to work with it. This
automatic conversion is called
coercion.
Ex.
6.12