Presentation is loading. Please wait.

Presentation is loading. Please wait.

Types(1). Lecture 52 Type(1)  A type is a collection of values and operations on those values. Integer type  values..., -2, -1, 0, 1, 2,...  operations.

Similar presentations


Presentation on theme: "Types(1). Lecture 52 Type(1)  A type is a collection of values and operations on those values. Integer type  values..., -2, -1, 0, 1, 2,...  operations."— Presentation transcript:

1 Types(1)

2 Lecture 52 Type(1)  A type is a collection of values and operations on those values. Integer type  values..., -2, -1, 0, 1, 2,...  operations +, -, *, /, <,... The Boolean type  has values true and false  operations   AND,   OR,   NOT.

3 Lecture 53 Type(2)  Computer types have a finite number of values due to fixed size allocation problematic for numeric types.  Exceptions: Smalltalk uses unbounded fractions. Haskell type Integer represents unbounded integers.  Fixed Size Floating point problems? 0.2 is not exact in binary. So 0.2 * 5 is not exactly 1.0 Floating point is inconsistent with real numbers in mathematics.

4 Lecture 54 Type(3)  In the early languages, Fortran, Algol, Cobol, all of the types were built in.  Purpose of types in programming languages is to provide ways of effectively modeling a problem solution. A type color, could use integers; but what does it mean to multiply two colors.

5 Lecture 55 Type Errors(1)  Machine data carries no type information.  Basically, just a sequence of bits.  Example: 0100 0000 0101 1000 0000 0000 0000 0000 The floating point number 3.375 The 32-bit integer 1,079,508,992 Two 16-bit integers 16472 and 0 Four ASCII characters  @ X NUL NUL

6 Lecture 56 Type Errors(2)  A type error is any error that arises because an operation is attempted on a data type for which it is undefined. Type errors are common in assembly language programming. High level languages reduce the number of type errors.  A type system provides a basis for detecting type errors.  Types constraints cannot be expressed syntactically in EBNF

7 Lecture 57 Type Errors Example  union {int a; float b;}  u.a =1;  …  x=x+u.p

8 Lecture 58 Static and Dynamic Typing(1)  A language is statically typed if the types of all variables are fixed when they are declared at compile time.  A language is dynamically typed if the type of a variable can vary at run time depending on the value assigned.

9 Lecture 59 Static and Dynamic Typing(2)  A language is strongly typed if its type system allows all type errors in a program to be detected either at compile time or at run time.  A strongly typed language can be either statically or dynamically typed.  Strongly typed languages Java  Non- Strongly typed languages C and C++

10 Lecture 510 Basic Types Issues  Binding types to memory is the compiler implementor task  Some languages allows to specify the required range for each variable  Numeric types is finite in size  overflow  Example a + (b + c)  (a + b) + c  C-like languages, the equality and relational operators produce an int, not a Boolean  Exact decimal 0.2 don’ have exact binary representation 5 x 0.2 ≠ 1.0

11 Lecture 511 Type conversion  At the machine level, arithmetic operations required that both operands be the same type  Type conversion Creating a different bit pattern to represent a value in a different type  A type conversion is a narrowing conversion if the result type permits fewer bits, thus potentially losing information.  Otherwise it is termed a widening conversion.

12 Lecture 512 Non-basic Types  Enumeration  Pointers  Arrays and Lists  Strings  Structures  Variant Records and Unions

13 Lecture 513 Enumeration : enum day {Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday}; enum day myDay = Wednesday;  In C/C++ the above values of this type are 0,..., 6.  Enumeration allows the code to become more readable.

14 Lecture 514 Pointers  A pointer is a value that represent a memory address  Pointers operators in C (unary &)  address of operator  Takes a variable argument  Returns the address of the variable (unary *)  dereferencing operator  Takes a reference  Produces the value of the reference

15 Lecture 515 Pointers and Arrays float sum(float a[ ], int n) { int i; float s = 0.0; for (i = 0; i<n; i++) s += a[i]; } float sum(float *a, int n) { int i; float s = 0.0; for (i = 0; i<n; i++) s += *a++; }

16 Lecture 516 Arrays and Lists  C-like languages restrict array to be one dimensional  More dimensional array is an array of arrays  Array size is statically bound Java creates array and the size can be determined at the runtime Ada the size of declared array is determine by the number of elements in its initial value

17 Lecture 517 Strings  In C, a string is a 1D array with the string value terminated by a NUL character (value = 0). User most remember to allocate at least 1 extra byte for trailing NUL character String assignment (a = b) is the programmer responsibility  In Java, Perl, Python, a string behaves as a built-in type. String variable can hold an unbounded number of characters.

18 Lecture 518 Structures  Collection of elements of different types  Used first in Cobol  Absent from Fortran  Common to Pascal-like and C-like languages  Omitted from Java as redundant

19 Lecture 519 Structure Example struct employeeType { int id; char name[25]; int age; float salary; char dept; }; struct employeeType employee;... employee.age = 45;

20 Lecture 520 Variant Record and Union  Two or more different fields share the same block of memory  Programmers had to keep the processing of the union variables separated through the run-time life of the program.  Languages that having union are not strongly-typed languages C++ is not strongly typed because of the backward compatibility with C  Less widely used on modern Applications


Download ppt "Types(1). Lecture 52 Type(1)  A type is a collection of values and operations on those values. Integer type  values..., -2, -1, 0, 1, 2,...  operations."

Similar presentations


Ads by Google