Presentation is loading. Please wait.

Presentation is loading. Please wait.

ISBN 0-321-19362-8 Chapter 6 Data Types Introduction Primitive Data Types User-Defined Ordinal Types.

Similar presentations


Presentation on theme: "ISBN 0-321-19362-8 Chapter 6 Data Types Introduction Primitive Data Types User-Defined Ordinal Types."— Presentation transcript:

1 ISBN 0-321-19362-8 Chapter 6 Data Types Introduction Primitive Data Types User-Defined Ordinal Types

2 Copyright © 2004 Pearson Addison-Wesley. All rights reserved.6-2 Introduction A data type defines a collection of data objects and a set of predefined operations on those objects Evolution of data types: –FORTRAN I (1957) - INTEGER, REAL, arrays –Ada (1983) - User can create a unique type for every category of variables in the problem space and have the system enforce the types –Object-Oriented languages have extended user- defined types even further

3 Copyright © 2004 Pearson Addison-Wesley. All rights reserved.6-3 Attributes of Variables A descriptor is the collection of the attributes of a variable –Type –Name –Other attributes are needed for structured data types

4 Copyright © 2004 Pearson Addison-Wesley. All rights reserved.6-4 Introduction Design issues for all data types: 1. What is the syntax of references to variables? 2. What operations are defined and how are they specified?

5 Copyright © 2004 Pearson Addison-Wesley. All rights reserved.6-5 Classes of Types Primitive - numeric, character, boolean Strings - may be either primitive or structured Structured types - arrays, records, … Pointers - reference types

6 Copyright © 2004 Pearson Addison-Wesley. All rights reserved.6-6 Primitive Data Types Primitive types are not defined in terms of other data types 1.Integer 2.Floating Point 3.Fixed Point 4.Character 5.Boolean

7 Copyright © 2004 Pearson Addison-Wesley. All rights reserved.6-7 Integer Types Almost always an exact reflection of the hardware, so the mapping is trivial There may be as many as eight different integer types in a language –Java has four integer types –C/C++ have the four that Java has plus four corresponding unsigned types Operations –Arithmetic –Comparison –Assignment

8 Copyright © 2004 Pearson Addison-Wesley. All rights reserved.6-8 Representing Integers Positive integers are easy - convert the number to base 2 How do you handle negative numbers when you have to use 0s and 1s? Several possibilities –Sign bit –Ones complement –Twos complement - this is the one that is used

9 Copyright © 2004 Pearson Addison-Wesley. All rights reserved.6-9 Representing negative integers Sign bit Ones complement

10 Copyright © 2004 Pearson Addison-Wesley. All rights reserved.6-10 Twos Complement To get the binary representation, take the complment and add 1

11 Copyright © 2004 Pearson Addison-Wesley. All rights reserved.6-11 Floating Point Types Model real numbers, but only as approximations Languages for scientific use support at least two floating-point types; sometimes more Usually exactly like the hardware, but not always Same operations as for integer types –Additional functions effectively provide other operations

12 Copyright © 2004 Pearson Addison-Wesley. All rights reserved.6-12 Representing Real Numbers We could convert the decimal number to base 2 just as we did for integers But, how do we represent the decimal point? Using a fixed number of bits for the whole and fractional parts severely limits the range of values we can represent Use a representation similar to scientific notation

13 Copyright © 2004 Pearson Addison-Wesley. All rights reserved.6-13 Floating Point Representation Normalize the number so the mantissa has the form 1.Fraction where Fraction is a sequence of binary bits. Use one bit to represent the sign (1 for negative) Use a fixed number of bits for the exponent which is offset to allow for negative exponents –Exponent = exponent + offset (-1) sign 1.Fraction x 2 Exponent IEEE has both single (32 bit) and double (64 bit) precision standards which are what is used in most modern hardware.

14 Copyright © 2004 Pearson Addison-Wesley. All rights reserved.6-14 IEEE Floating Point Formats IEEE 754 : (-1) sign 1.Fraction x 2 Exponent-offset

15 Copyright © 2004 Pearson Addison-Wesley. All rights reserved.6-15 Decimal Types Also called fixed point types For business applications (money) Store a fixed number of decimal digits (coded) Advantage: accuracy Disadvantages: limited range, wastes memory

16 Copyright © 2004 Pearson Addison-Wesley. All rights reserved.6-16 Other Primitive Types Boolean –Could be implemented as bits, but often as bytes –Advantage: readability Character –Stored as numeric codings (e.g., ASCII, Unicode) Others –Fortran has a complex type –Scheme has a rational type

17 Copyright © 2004 Pearson Addison-Wesley. All rights reserved.6-17 User-Defined Ordinal Types An ordinal type is one in which the range of possible values can be easily associated with the set of positive integers –Enumeration types –Subrange types

18 Copyright © 2004 Pearson Addison-Wesley. All rights reserved.6-18 Enumeration Types The user enumerates all of the possible values, which are symbolic constants Design Issue: Should a symbolic constant be allowed to be in more than one type definition?

19 Copyright © 2004 Pearson Addison-Wesley. All rights reserved.6-19 Enumeration Types Examples: –Pascal - cannot reuse constants; they can be used for array subscripts, for variables, case selectors; NO input or output; can be compared –Ada - constants can be reused (overloaded literals); distinguish with context or type_name ‘ (one of them); can be used as in Pascal; CAN be input and output –C and C++ - like Pascal, except they can be input and output as integers –Java does not include an enumeration type, but provides the Enumeration interface

20 Copyright © 2004 Pearson Addison-Wesley. All rights reserved.6-20 Enumerated types in C (and C++) Use the enum keyword to define an enumerated type in C enum weekday {Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday}; enum weekday today = Tuesday; Using typedef makes the enum type easier to use typedef enum weekday {Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday} weekday; weekday today = Tuesday; By default, the names in the list are numbered consecutively from 0. –You can explicitly assign values Enum months {January=1, February, …}; Unless you are happy with the int values, you have to write code for input and output.

21 Copyright © 2004 Pearson Addison-Wesley. All rights reserved.6-21 Enumerations in Java 1.5 An enum is a new class which extends java.lang.Enum and implements Comparable –Get type safety and compile-time checking –Implicitly public, static and final –Can use either == or equals to compare –toString and valueOf are overridden to make input and output easier

22 Copyright © 2004 Pearson Addison-Wesley. All rights reserved.6-22 Java enum Example Defining an enum type enum Season {WINTER, SPRING, SUMMER, FALL}; Declaring an enum variable Season season = Season.WINTER; toString gives you the string representation of the name System.out.println( season); // prints WINTER valueOf lets you convert a String to an enum Season = valueOf(“SPRING”);

23 Copyright © 2004 Pearson Addison-Wesley. All rights reserved.6-23 Enumeration Types Evaluation (of enumeration types): a. Aid to readability--e.g. no need to code a color as a number b. Aid to reliability--e.g. compiler can check: i. operations (don’t allow colors to be added) ii. ranges of values (if you allow 7 colors and code them as the integers, 1..7, then 9 will be a legal integer (and thus a legal color))

24 Copyright © 2004 Pearson Addison-Wesley. All rights reserved.6-24 Subrange Types A subrange type is an ordered contiguous subsequence of an ordinal type 1..10 Monday..Friday Design Issues: –How can they be used? –Can they be mixed with other types in expressions? –What are type compatibility rules?

25 Copyright © 2004 Pearson Addison-Wesley. All rights reserved.6-25 Subrange Types in Pascal Subrange types behave as their parent type Example type pos = 0.. MAXINT; Subrange types and enum types can be used as for variables and array indices Array declarations use a subrange type as the index range var grades : array[1..25] of integer; hours : array[Monday..Friday] of real;

26 Copyright © 2004 Pearson Addison-Wesley. All rights reserved.6-26 Subrange Types in Ada Subtypes are not new types, just constrained existing types – they are compatible with the original type can be used as in Pascal, and as case constants Example subtype POS_TYPE is INTEGER range 0..INTEGER'LAST;

27 Copyright © 2004 Pearson Addison-Wesley. All rights reserved.6-27 Subrange Types Evaluation of subrange types: –Aid to readability –Reliability - restricted ranges add error detection

28 Copyright © 2004 Pearson Addison-Wesley. All rights reserved.6-28 Implementation of Ordinal Types Enumeration types are often implemented as integers –Type safety is an issue Subrange types are the parent types with code inserted (by the compiler) to restrict assignments to subrange variables –Rules for type compatibility vary

29 Copyright © 2004 Pearson Addison-Wesley. All rights reserved.6-29 What’s next? Strings Structured data types Pointers


Download ppt "ISBN 0-321-19362-8 Chapter 6 Data Types Introduction Primitive Data Types User-Defined Ordinal Types."

Similar presentations


Ads by Google