Data Types Data Types Every program uses data, either explicitly or - - PowerPoint PPT Presentation

data types data types
SMART_READER_LITE
LIVE PREVIEW

Data Types Data Types Every program uses data, either explicitly or - - PowerPoint PPT Presentation

Data Types Data Types Every program uses data, either explicitly or implicitly to arrive at a result. Data in a program is collected into data structures, and is manipulated by algorithms. Algorithms + Data Structures = Programs 2 Data


slide-1
SLIDE 1

Data Types

slide-2
SLIDE 2

Data Types

Every program uses data, either explicitly or implicitly to arrive at a result. Data in a program is collected into data structures, and is manipulated by algorithms. Algorithms + Data Structures = Programs

2

slide-3
SLIDE 3

Data Types

Most programming languages provide:

A set of simple data entities such as

Integer Real Boolean Char

Mechanisms for constructing new data entities from these.

3

slide-4
SLIDE 4

Data Types

  • Finiteness:

– Mathematically, integer data is thought of as:

  • …, -3, -2, -1, 0, 1, 2, 3, …
  • In a computer’s hardware, there is always an upper

and lower bound for values of integers.

  • This is due to a given representation size in bits of an

integer on a specific machine.

4

slide-5
SLIDE 5

Data Types

 Different language designs deal with data type information in different ways.  Some languages mandate that type information be explicit in the programming language. This is used to verify program correctness prior to execution. Example:

int x; float y; char z;

5

slide-6
SLIDE 6

Data Types

Scheme is a language with no explicit types or translation-time typing. Ada is a very strictly-typed language. It performs strict type checking.

6

slide-7
SLIDE 7

Data Types

 Some form of static (translation-time) type checking is very useful to:

 Allocate memory efficiently.  Reduce the amount of code to be compiled.  Improves both writability and readability.  Improves security and reliability.  Removes ambiguities.  Can be used to prove program correctness.

7

slide-8
SLIDE 8

Data Types

A Data Type is a Set of Values Insufficient Definition!

8

slide-9
SLIDE 9

Data Types

A Data Type is a Set of Values + A Set of Operations

  • n

Those Values having Certain Properties

9

slide-10
SLIDE 10

Data Types

Example:

The values of an int range from a lower bound to an upper bound (e.g. MININT to MAXINT) Operations on int include:

+ - * / Etc.

10

slide-11
SLIDE 11

Data Types

Type Checking:

Is the process a translator goes through to determine whether the type information in a program is consistent.

11

slide-12
SLIDE 12

Data Types

Type Checking:

Example:

x = y + z Can the type of the result of y + z be stored in x? The type of y + z depends on the type of y and z Determining the type of y + z is called type inference.

12

slide-13
SLIDE 13

Data Types

We can construct more complex user defined types from simple data types: Example:

An array of ten integers in C/C++:

int a[10]

The same array in Ada:

a: array (1..10) of integer

13

slide-14
SLIDE 14

Data Types

Type definitions:

In some languages, we can give new types names. Example in C:

typedef int Array_of_ten_integers[10]; Array_of_ten_integers a;

A name given to an int array

  • f ten elements

Just like we declare int a, We can declare Array_of_ten_integers a

14

slide-15
SLIDE 15

Data Types

Type definitions:

In some languages, we can give new types names. Example in C:

typedef int Array_of_ten_integers[10]; Array_of_ten_integers a;

A name given to an int array

  • f ten elements

Just like we declare int a, We can declare Array_of_ten_integers a

15

slide-16
SLIDE 16

Data Types

Type definitions:

In some languages, we can give new types names. Example in Ada:

type Array_of_ten_integers is array(1..10) of integer; a: Array_of_ten_integers;

16

slide-17
SLIDE 17

Data Types

 Type definitions:

With new type definitions, we have a problem ! During type checking, a translator must often compare two types to determine if they are the same. Example: The following should be equivalent in type!

 int a[10]  Array_of_ten_integers

17

slide-18
SLIDE 18

Data Types

Type definitions:

Each language with type declarations has rules for checking equivalence. These are called rule equivalence algorithms.

18

slide-19
SLIDE 19

Data Types

 Strongly Typed Languages:

If a programming language definition specifies a complete type system that can be applied statically. Guarantees that all (unintentional) data-corrupting errors in a program will be detected at the earliest possible point. The language is said to be strongly typed.

19

slide-20
SLIDE 20

Data Types

Strongly Typed Languages:

All type errors are detected at translation time. With the exception of a few errors that can only be checked during execution.

E.g. array subscript bounds

20

slide-21
SLIDE 21

Data Types

 Strongly Typed Languages:

Strong typing ensures that:

 Most unsafe programs (programs with data corrupting errors) will be rejected at translation time.  The unsafe programs that are not rejected at translation time will cause an execution error prior to any data corrupting actions.

Strong typing can also be a burden on the programmer, having to explicitly specify types all the time.

21

slide-22
SLIDE 22

Data Types

Strongly Typed Languages (Examples):

Ada is a strongly typed language. ML and Haskell Pascal C (Considered weakly typed)

Strong Weak

22

slide-23
SLIDE 23

Data Types

 Untyped Languages:

 Languages without static type checking.  Also called dynamically typed languages.  Examples: Scheme, Smalltalk, most scripting languages such as Perl.  An untyped language does not necessarily allow programs to corrupt data, but rather all safety checking is performed at execution time:

 They will generate runtime errors.

23

slide-24
SLIDE 24

Data Types

Type Construction:

How are new types constructed? We can think of construction as mathematical

  • perations.

24

slide-25
SLIDE 25

Data Types

Type Construction:

Cartesian product. Unions. Subsets. Arrays/Functions. Sequences/Lists. Recursive types. No Intersection: types should NOT overlap.

25

slide-26
SLIDE 26

Data Types

 Type Construction:

Cartesian product:

 Finite combinations of previously defined types.  Example:

C structs: struct IntCharReal { int i; char c; double r; }; A finite combination of those types

26

slide-27
SLIDE 27

Data Types

Type Construction:

Unions:

Values belong to ONE of a finite set of types. Example:

C unions: union IntOrReal { int i; double r; };

Only one of those can be used.

27

slide-28
SLIDE 28

Data Types

Type Construction:

Subsets:

A new type may be a subrange of another type. Example in Ada:

subtype IntDigit_Type is integer range 0..9

28

slide-29
SLIDE 29

Data Types

Type Construction:

Subsets:

A new type may be a subrange of another type. Example in Ada:

subtype IntDigit_Type is integer range 0..9

29

slide-30
SLIDE 30

Data Types

Type Construction:

Arrays and Functions:

You can define arrays in most programming languages as a homogeneous collection of a given type. You can define functions also.

30

slide-31
SLIDE 31

Data Types

Type Construction:

Sequences and Lists:

Sequences are like arrays, except that they are potentially infinite.

Example: Streams

Lists are like arrays, but can only be accessed by counting down from the first element.

Almost all functional languages have lists.

31

slide-32
SLIDE 32

Data Types

Type Construction:

Recursive Types:

Example: class node { int value; node next; }

Recursive Definition

32

slide-33
SLIDE 33

Data Types

 Example: Java Type Structure:

Java Types Numeric class interface double boolean short Integral float Floating point int char Primitive Reference Array long byte

33

slide-34
SLIDE 34

Data Types

 Example: C Type Structure:

C Types Numeric Pointer Function struct union double void long double short int Integral float Floating enum int char (signed) (unsigned) Basic Derived Array long int

34

slide-35
SLIDE 35

Data Types

 Type Checking:

Dynamic type checking:

 If type information is maintained and checked at runtime.  Interpreters by definition perform dynamic type checking.  Compilers also generate code that maintain type attributes during runtime in table.  Dynamic type checking is required when the types can only be determined at runtime.

Static type checking:

 The types are determined from the text of the program.  Checking is performed before execution.

35

slide-36
SLIDE 36

Data Types

 Type Checking:

A strongly typed language must report all type errors as compilation errors that prevent execution. A language definition may not specify whether static or dynamic type checking is to be used.

36

slide-37
SLIDE 37

Data Types

 Type Compatibility:

Two different types may be combined correctly in certain ways. They are called compatible. Example: int, short. What do we mean by combined correctly in a certain way?

 Ranges!

37

slide-38
SLIDE 38

Data Types

 Implicit Types:

Sometimes types are implicitly inferred. Example: 2 + 3, or x / y In C, the following is an implicit int variable

 X;

Also, in C, if functions have no return type indicated, they implicitly return an int.

38

slide-39
SLIDE 39

Data Types

 Overlapping Types:

It is sometimes difficult to avoid overlapping of various types. Example: In Java, the following types overlap:

 int  short  long  byte  All overlap

39

slide-40
SLIDE 40

Data Types

  • Shared Operations:

– The + operator for example may be applied to any combination

  • f int, short, byte, double, float, long.

– The + operator even has a different meaning in:

  • System.out.println(“My” + “Name” + “Is”);

– The plus operator is said to be overloaded.

40

slide-41
SLIDE 41

Data Types

 Type Conversion:

There is always a need to convert from one type to another. Conversion can either be implicit (automatic) or explicit (manual).

41

slide-42
SLIDE 42

Data Types

  • Type Conversion:

– Example of implicit type conversion in C/C++:

  • int x = 3.3;
  • Although x is an int, 3.3 is truncated to 3, and stored in x.

– Example of implicit type conversion in Java:

  • double x = 2 + 3;
  • The expression’s type is automatically promoted to double to be

stored in x.

– Implicit conversions are also called coercions.

42

slide-43
SLIDE 43

Data Types

 Type Conversion:

When we implicitly convert from int to double for example, the destination is large enough to accommodate any int. This is called a widening conversion. The opposite is called a narrowing conversion.

43

slide-44
SLIDE 44

Polymorphic Type Checking

 Most statically typed languages require that explicit type information be given for all names in each declaration.  It is possible to use an extension of type inference to determine the types of names in a declaration without explicitly giving those types.  The translator can collect information on uses of a name, and infer from the set of all uses a probable type (the most general type) for which all uses are correct.  The Hindley-Milner type checking provides this kind of inference. It is a major feature of strongly typed functional languages such as ML and Haskell.

44

slide-45
SLIDE 45

Data Types

 Type Conversion:

Explicit conversions involve the writing of directives to explicitly convert from one type to another in the code. Example:

 int x = 2;  double y = 3.3;  y = (double) x; (type casting)

Type casting will temporarily change the type of the RHS to that

  • f the cast.

45

slide-46
SLIDE 46