data types data types
play

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


  1. Data Types

  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

  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

  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

  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

  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

  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

  8. Data Types A Data Type is a Set of Values Insufficient Definition! 8

  9. Data Types A Data Type is a Set of Values + A Set of Operations on Those Values having Certain Properties 9

  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

  11. Data Types  Type Checking:  Is the process a translator goes through to determine whether the type information in a program is consistent. 11

  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

  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

  14. Data Types  Type definitions:  In some languages, we can give new types names.  Example in C: A name given to an int array of ten elements typedef int Array_of_ten_integers[10]; Array_of_ten_integers a; Just like we declare int a, We can declare Array_of_ten_integers a 14

  15. Data Types  Type definitions:  In some languages, we can give new types names.  Example in C: A name given to an int array of ten elements typedef int Array_of_ten_integers[10]; Array_of_ten_integers a; Just like we declare int a, We can declare Array_of_ten_integers a 15

  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

  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

  18. Data Types  Type definitions:  Each language with type declarations has rules for checking equivalence.  These are called rule equivalence algorithms. 18

  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

  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

  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

  22. Data Types  Strongly Typed Languages (Examples): Strong  Ada is a strongly typed language.  ML and Haskell  Pascal Weak  C (Considered weakly typed) 22

  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

  24. Data Types  Type Construction:  How are new types constructed?  We can think of construction as mathematical operations. 24

  25. Data Types  Type Construction:  Cartesian product.  Unions.  Subsets.  Arrays/Functions.  Sequences/Lists.  Recursive types.  No Intersection: types should NOT overlap. 25

  26. Data Types  Type Construction:  Cartesian product:  Finite combinations of previously defined types.  Example:  C structs: struct IntCharReal { A finite combination of int i; those types char c; double r; }; 26

  27. Data Types  Type Construction:  Unions:  Values belong to ONE of a finite set of types.  Example:  C unions: union IntOrReal Only one of those can { be used. int i; double r; }; 27

  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

  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

  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

  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

  32. Data Types  Type Construction:  Recursive Types:  Example: class node { int value; node next; Recursive Definition } 32

  33. Data Types  Example: Java Type Structure: Java Types Primitive Reference Numeric Array boolean class interface Floating point Integral char float byte double short int long 33

  34. Data Types  Example: C Type Structure: C Types Basic Derived void Numeric Pointer Array Function struct union Integral Floating float double long double (signed) enum (unsigned) char int short int long int 34

  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

  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

  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

  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

  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

  40. Data Types • Shared Operations: – The + operator for example may be applied to any combination of 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

  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

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend