isbn 0-321-19362-8 chapter 6 data types introduction primitive data types user-defined ordinal types

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

Upload: jocelyn-walsh

Post on 05-Jan-2016

213 views

Category:

Documents


1 download

TRANSCRIPT

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

ISBN 0-321-19362-8

Chapter 6

Data Types•Introduction•Primitive Data Types•User-Defined Ordinal Types

Page 2: ISBN 0-321-19362-8 Chapter 6 Data Types Introduction Primitive Data Types User-Defined Ordinal Types

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

Page 3: ISBN 0-321-19362-8 Chapter 6 Data Types Introduction Primitive Data Types User-Defined Ordinal Types

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

Page 4: ISBN 0-321-19362-8 Chapter 6 Data Types Introduction Primitive Data Types User-Defined Ordinal Types

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?

Page 5: ISBN 0-321-19362-8 Chapter 6 Data Types Introduction Primitive Data Types User-Defined Ordinal Types

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

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

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

Page 7: ISBN 0-321-19362-8 Chapter 6 Data Types Introduction Primitive Data Types User-Defined Ordinal Types

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

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

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

Page 9: ISBN 0-321-19362-8 Chapter 6 Data Types Introduction Primitive Data Types User-Defined Ordinal Types

Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 6-9

Representing negative integers

• Sign bit

• Ones complement

Page 10: ISBN 0-321-19362-8 Chapter 6 Data Types Introduction Primitive Data Types User-Defined Ordinal Types

Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 6-10

Twos Complement

• To get the binary representation, take the complment and add 1

Page 11: ISBN 0-321-19362-8 Chapter 6 Data Types Introduction Primitive Data Types User-Defined Ordinal Types

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

Page 12: ISBN 0-321-19362-8 Chapter 6 Data Types Introduction Primitive Data Types User-Defined Ordinal Types

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

Page 13: ISBN 0-321-19362-8 Chapter 6 Data Types Introduction Primitive Data Types User-Defined Ordinal Types

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 2Exponent

• IEEE has both single (32 bit) and double (64 bit) precision standards which are what is used in most modern hardware.

Page 14: ISBN 0-321-19362-8 Chapter 6 Data Types Introduction Primitive Data Types User-Defined Ordinal Types

Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 6-14

IEEE Floating Point Formats

• IEEE 754 : (-1)sign 1.Fraction x 2Exponent-offset

Page 15: ISBN 0-321-19362-8 Chapter 6 Data Types Introduction Primitive Data Types User-Defined Ordinal Types

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

Page 16: ISBN 0-321-19362-8 Chapter 6 Data Types Introduction Primitive Data Types User-Defined Ordinal Types

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

Page 17: ISBN 0-321-19362-8 Chapter 6 Data Types Introduction Primitive Data Types User-Defined Ordinal Types

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

Page 18: ISBN 0-321-19362-8 Chapter 6 Data Types Introduction Primitive Data Types User-Defined Ordinal Types

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?

Page 19: ISBN 0-321-19362-8 Chapter 6 Data Types Introduction Primitive Data Types User-Defined Ordinal Types

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

Page 20: ISBN 0-321-19362-8 Chapter 6 Data Types Introduction Primitive Data Types User-Defined Ordinal Types

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 usetypedef 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 valuesEnum months {January=1, February, …};

• Unless you are happy with the int values, you have to write code for input and output.

Page 21: ISBN 0-321-19362-8 Chapter 6 Data Types Introduction Primitive Data Types User-Defined Ordinal Types

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

Page 22: ISBN 0-321-19362-8 Chapter 6 Data Types Introduction Primitive Data Types User-Defined Ordinal Types

Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 6-22

Java enum Example

• Defining an enum typeenum Season {WINTER, SPRING, SUMMER, FALL};

• Declaring an enum variableSeason season = Season.WINTER;

• toString gives you the string representation of the nameSystem.out.println( season);

// prints WINTER

• valueOf lets you convert a String to an enumSeason = valueOf(“SPRING”);

Page 23: ISBN 0-321-19362-8 Chapter 6 Data Types Introduction Primitive Data Types User-Defined Ordinal Types

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))

Page 24: ISBN 0-321-19362-8 Chapter 6 Data Types Introduction Primitive Data Types User-Defined Ordinal Types

Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 6-24

Subrange Types

• A subrange type is an ordered contiguous subsequence of an ordinal type1..10

Monday..Friday

• Design Issues: – How can they be used?

– Can they be mixed with other types in expressions?

– What are type compatibility rules?

Page 25: ISBN 0-321-19362-8 Chapter 6 Data Types Introduction Primitive Data Types User-Defined Ordinal Types

Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 6-25

Subrange Types in Pascal

• Subrange types behave as their parent type

• Exampletype 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 rangevar grades : array[1..25] of integer;

hours : array[Monday..Friday] of real;

Page 26: ISBN 0-321-19362-8 Chapter 6 Data Types Introduction Primitive Data Types User-Defined Ordinal Types

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;

Page 27: ISBN 0-321-19362-8 Chapter 6 Data Types Introduction Primitive Data Types User-Defined Ordinal Types

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

Page 28: ISBN 0-321-19362-8 Chapter 6 Data Types Introduction Primitive Data Types User-Defined Ordinal Types

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

Page 29: ISBN 0-321-19362-8 Chapter 6 Data Types Introduction Primitive Data Types User-Defined Ordinal Types

Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 6-29

What’s next?

• Strings• Structured data types• Pointers