Download presentation
Presentation is loading. Please wait.
1
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 5 Types Types are the leaven of computer programming; they make it digestible. Robin Milner
2
Copyright © 2006 The McGraw-Hill Companies, Inc. 5.1Type Errors 5.2Static and Dynamic Typing 5.3Basic Types 5.4Non-Basic Types-through 5.4.2 5.5Recursive Data Types 5.6Functions as Types 5.7Type Equivalence 5.8Subtypes 5.9Polymorphism and Generics 5.10Programmer-Defined Types
3
Copyright © 2006 The McGraw-Hill Companies, Inc. What is a Type? A type is a collection of values and operations on those values. Example: Integer type has values..., -2, -1, 0, 1, 2,... and operations +, -, *, /, <,... The Boolean type has values true and false and operations (&&), (||), (!). Most types have a finite number of values due to fixed size allocation (e.g., can’t express all integers) Exception: some languages (Java, Haskell) support unlimited integer sizes
4
Copyright © 2006 The McGraw-Hill Companies, Inc. How Many Types Are Needed? Early languages, (Fortran, Algol, Cobol), built in all types – no way to define new ones. If you needed a type color, you could use integers; But – there’s no way to prevent illegal operations. In programming languages, types are used to provide ways of effectively modeling a problem solution, so the user should be able to define new types.
5
Copyright © 2006 The McGraw-Hill Companies, Inc. 5.1 Type Errors Machine data carries no type information. Example: 0100 0000 0101 1000 0000 0000 0000 0000 could represent –3.375 (floating point) –1,079,508,992 (a 32-bit integer) –16472 & 0 (two 16-bit integers) –The ASCII characters @ X NUL NUL depending on the interpretation
6
Copyright © 2006 The McGraw-Hill Companies, Inc. 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 defines the bindings between a variable’s type, values, and the operations that can be performed on the values. –provides a basis for detecting type errors.
7
Copyright © 2006 The McGraw-Hill Companies, Inc. 5.2 Static and Dynamic Typing A type system imposes constraints that cannot be expressed syntactically in BNF, EBNF, or any other notation system for context-free grammars. Type checking is part of semantic processing –Some languages perform type checking at compile time (e.g, C/C++). (compile-time semantics) –Others (e.g., Perl, Python) perform type checking at run time. –Still others (e.g., Java) do both.
8
Copyright © 2006 The McGraw-Hill Companies, Inc. Definitions 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
Copyright © 2006 The McGraw-Hill Companies, Inc. Definitions (continued) 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. –If a language detects all type errors we sometimes say it exhibits type safety –Alternate definitions of strong typing exist, however A strongly typed language should know (either at compile time or run time) the type of every variable, constant, or function return value. –Based on this knowledge, type errors can be detected – for example, trying to assign a float value to a bool variable would be detected.
10
Copyright © 2006 The McGraw-Hill Companies, Inc. Definitions (continued) A strongly typed language can be either statically (Ada, Java) or dynamically (Scheme, Perl) typed –(except some definitions of strong typing require types to be statically declared.) C/C++ are usually not considered strongly typed. –e.g., it allows the assignment of a char value to an int variable; it doesn’t check for out-of-bounds array references, …. Strong typing promotes reliability, limits undefined runtime behavior.
11
Copyright © 2006 The McGraw-Hill Companies, Inc. 5.3 Basic Types Consist of atomic values (e.g., floats) Sometimes called simple or primitive types. Traditionally, correspond for the most part to data types provided by computers and occupy fixed amounts of memory, based on computer memory structure.
12
Copyright © 2006 The McGraw-Hill Companies, Inc. Common Memory Sizes for the Basic Types (On 32-bit Computers) Nibble: 4 bits Byte: 8 bits Half-word: 16 bits Word: 32 bits Double word: 64 bits Quad word: 128 bits In most languages, simple data types are limited to one of these sizes
13
Copyright © 2006 The McGraw-Hill Companies, Inc. Basic Types in C, Ada, Java TypeCAdaJava Byte------ byte Integer short, int, long integershort, int, long Real float, doublefloat, decimalfloat, double Character charcharacterchar Boolean --- booleanboolean
14
Copyright © 2006 The McGraw-Hill Companies, Inc. Memory Requirements In most languages the binding of type to memory size is implementation dependent. For example, in C/C++ the only language restriction is sizeOf(short)<= sizeOf(int)<= sizeOf(long) Java defines size of each (half-word, word, double word)
15
Copyright © 2006 The McGraw-Hill Companies, Inc. Floating Point Representation Floating point numbers (reals) are expressed as the product of a binary number and a power of 2. IEEE standard is used on computers designed/built after 1980 If s = sign bit, e = exponent (in excess 127 notation) and m is the mantissa, a single precision representation means (-1) s x 1.m x 2 e-127
16
Copyright © 2006 The McGraw-Hill Companies, Inc. IEEE 754 Floating Point Number Representation Figure 5.1 (-1) s x 1.m x 2 e-127
17
Copyright © 2006 The McGraw-Hill Companies, Inc. Floating Point Problems Integers are exact, floats aren’t necessarily so. –e.g., since 0.2 cannot be represented exactly as a binary fraction, the floating point representation is not exact; result: 0.2 * 5 is not exactly 1.0 Overflow and underflow are possible Inaccuracies due to floating point arithmetic may affect results; for example, it’s possible that a + (b + c) ≠ (a + b) + c
18
Copyright © 2006 The McGraw-Hill Companies, Inc. Example : a = -1324x10 3, b = 1325 x 10 3, c = 5424 x 10 0 Assume 4 significant figures for the fraction. (a + b) = -1324 x 10 3 + 1325 x 10 3 = 1 x 10 3 (a + b) + c = 1000 x 10 0 + 5424 x 10 0 = 6424 x 10 0 Whereas (b + c) = 1325 x 10 3 + 0005 x 10 3 = 1330 x 10 3 a + (b + c) = -1324 x 10 3 + 1330 x 10 3 = 6 x 10 3 = 6000 x 10 0 This is sometimes called representational error
19
Copyright © 2006 The McGraw-Hill Companies, Inc. Overflow Arithmetic operations may overflow the finite memory allocation and give an incorrect result, regardless of whether the values are integers or floats. Some machines generate an exception when this happens, some (e.g., Java Virtual Machine) don’t.
20
Copyright © 2006 The McGraw-Hill Companies, Inc. Java’s BigInteger and BigDecimal type http://java.sun.com/j2se/1.5.0/docs/api/java/math/BigInteger.html arbitrary-precision integers, semantics of arithmetic operations exactly mimic those of Java's integer arithmetic operators http://java.sun.com/j2se/1.5.0/docs/api/java/math/BigDecimal.html arbitrary-precision signed decimal numbers, consist of an arbitrary precision integer unscaled value and a 32-bit integer scale. Some applications; e.g., computer security, need very large numbers.
21
Copyright © 2006 The McGraw-Hill Companies, Inc. Operator Overloading An operator or function is overloaded when its meaning varies depending on the types of its operands or arguments or result. More about function overloading later Java: a + b –floating point add –integer add –string concatenation What if a is an integer and b is a float?
22
Copyright © 2006 The McGraw-Hill Companies, Inc. Mixed Mode Expressions Mixed mode: operands have different types; e.g., one operand an int, the other floating point (Also sometimes called mixed type) Why is it a problem? (Difference in hardware operations) Most languages have type rules that define how mixed mode expressions will be treated.
23
Copyright © 2006 The McGraw-Hill Companies, Inc. Type Conversions A type conversion is a narrowing conversion if the result type permits fewer bits, thus potentially losing information. Narrowing: float to int ( 2.4 to 2, for example ), or long to short Otherwise it is termed a widening conversion.
24
Copyright © 2006 The McGraw-Hill Companies, Inc. Implicit v Explicit Type Conversions Language design decision: Should type conversions be done automatically by the compiler (implicit conversions) or should the language require a cast (explicit conversion/casts)? Consensus: OK to have implicit widening conversions; narrowing conversions should be explicit.
25
Copyright © 2006 The McGraw-Hill Companies, Inc. Implicit v Explicit Type Conversions C++ example – widening int sum; int n; float av; av = sum/n; C++ example – narrowing float n1; float n2; int sum; sum = n1 + n2;
26
Copyright © 2006 The McGraw-Hill Companies, Inc. 5.4 Nonbasic Types Nonbasic types: Composed from other data types (e.g., arrays) Also called structured types Size is not fixed by the language but is programmer determined.
27
Copyright © 2006 The McGraw-Hill Companies, Inc. 5.4.1 Enumeration Types Provided by many languages as a way to improve program readability Essentially, allow the programmer to assign names to integer values and then use the names in programming (e.g., color, shape, day, …)
28
Copyright © 2006 The McGraw-Hill Companies, Inc. Enumeration Types – C/C++ Example : enum Day {Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday}; enum Day myDay = Wednesday; Values can be compared: if (myDay == Sunday)... Implicit enumeration-type-to-integer conversion is okay; use a cast to go the other direction: for(myDay=Monday;myDay<Friday;day(myDay+1))
29
Copyright © 2006 The McGraw-Hill Companies, Inc. Enumeration Types - Java More powerful than C/C++ version because the Enum class inherits methods from the Java class Object, such as toString. for (day d : day.values()) System.out.println(d);
30
Copyright © 2006 The McGraw-Hill Companies, Inc. 5.4.2 Pointers Design decision: to have pointers, or not? C, C++, Ada, Pascal Java??? History Early languages (Fortran, COBOL): static memory Later languages added pointers for dynamic memory allocation (Pascal, C, C++, Ada) Modern languages like Java do not have explicit pointers but still support dynamic memory allocation.
31
Copyright © 2006 The McGraw-Hill Companies, Inc. Pointer Operations The value of a pointer is a memory address. C/C++ operators: address-of (unary & ) returns the address of a variable dereferencing (unary *) returns the value of a reference (when used as an R-value) –Corresponds to indirect addressing mode in machine code For an arbitrary variable x : *(&x) = x
32
Copyright © 2006 The McGraw-Hill Companies, Inc. C/C++ Examples – Linked List Definitions struct Node { int key; struct Node* next; }; struct Node* head; Node is a recursive data structure
33
Copyright © 2006 The McGraw-Hill Companies, Inc. Fig 5.4: A Simple Linked List in C/C++
34
Copyright © 2006 The McGraw-Hill Companies, Inc. Pointer Problems Error-prone (mistakes in usage) Buffer overflow, memory leaks (from non-freed dynamic memory) Particularly troublesome in C/C++, in part because of the duality between pointers and array references
35
Copyright © 2006 The McGraw-Hill Companies, Inc. C Array/Pointer Example with Dereferencing float sum(float a[ ], int n) { int i; float s = 0.0; for (i = 0; i<n; i++) s += a[i]; return s; float sum(float *a, int n) { int i; float s = 0.0; for (i = 0; i<n; i++) s += *a++; return s;
36
Copyright © 2006 The McGraw-Hill Companies, Inc. void strcpy(char *p, char *q) { while (*p++ = *q++) ; } Strings p and q are arrays of characters ending in a NUL character, which is interpreted as false. (Note that if p is shorter than q, the array will be overrun. strcpy: A “bad” C example
37
Copyright © 2006 The McGraw-Hill Companies, Inc. Reference Types in Java In Java, all nonprimitive types are reference types A variable from a reference type stores the address of a memory object; i.e., it is the equivalent of a pointer variable (may depend on JVM implementation). References differ from pointers in that they don’t require specific dereferencing operations. Java arrays and classes are reference types.
38
Copyright © 2006 The McGraw-Hill Companies, Inc. Preview … Next time: arrays structs subtypes parametric polymorphism and generics
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.