CSCI 431 Lecture Notes - Elementary Data Types

Data Types

programs: a set of operations  working on certain data in a certain sequence.

Differences among languages are in the

  1. types of data allowed,
  2. types of operations available, and
  3. mechanisms for sequence control.


chapter 4:  data types that are built into languages
chapter 5:  programmer-defined data types (encapsulated data types)
chapter 6:  individual operation and sequence control
chapter 7:  subprogram control
chapter 8:  introduces inheritance (operations on encapsulated data objects can be automatically derived)
 

Properties of Data Types and Objects

It is important to differentiate between:

Data Objects

A data object has various attributes that define that object. The value that is bound to some of these attributes may change during runtime. Some of the most important of these include:


Examples of data objects are:

Data Types

A data type is a class of data objects together with a set of operations for creating and manipulating them. Each language has a set of primitive data types built into the language. Each data type has a specification that is made up of

A data type also has an implementation, made up of

A program deals with particular data objects, but a programming language deals more commonly with data types and the operations provided for manipulating them.

Every programming language has a set of primitive (or elementary) data types that are built into the language.  In addition, a language may provide facilities to allow the programmer to define new data types, such as C and Ada. In other words, there are two kinds of data types

Specification of Elementary Data Types

An elementary data type is an elementary data objects plus operations. Elementary data objects may store only one value, i.e. integer, boolean, character.


Implementation of Elementary Data Types

Declarations

A declaration is a program statement commonly used to specify the name and type of data objects

Declarations of Operations: to specify the signatures of operations to the language translator

There are also type declarations. e.g.,


    typedef struct{
      int number;
      char name[NAME_LEN+1];
    } Part;

Purposes for Declarations

Type Checking and Type Conversion

Each memory location contains some binary string. By that information alone, it is impossible to tell the type of the data object stored in that location. A computer could try to add two integers, whereas they may actually record two reals, leading to a garbage result.

Type checking is the process of ensuring that the arguments to an expression are of correct types. In other words, to ensure that each operation receives the proper number of arguments of the proper type. This may be done dynamically, but this requires that each data object stores its type, and that each time an operation is performed the type is checked. Most languages minimise dynamic type checking by doing it statically at compile time.

Type Equivalence

When are two types equivalent?

Structural Equivalence Name Equivalence

Example


        type  alink = pointer to cell;
        type blink = alink;
        p, q : pointer to cell;
        r    : alink;
        s    : blink;
        t    : pointer to cell;
        u    : alink;

Strong typing

Strong typing has been defined in different ways. We will take it to mean the following:
A language is strongly typed if that language prevents you from applying an operation to data for which it is not appropriate.

A related definition: A function f, with signature f : S -> R, is type safe if execution of f cannot generate a value outside of R

Type conversion and coercion

Assignment and Initialization