Download - 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
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
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
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?
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
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
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
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
Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 6-9
Representing negative integers
• Sign bit
• Ones complement
Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 6-10
Twos Complement
• To get the binary representation, take the complment and add 1
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
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
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.
Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 6-14
IEEE Floating Point Formats
• IEEE 754 : (-1)sign 1.Fraction x 2Exponent-offset
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
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
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
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?
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
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.
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
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”);
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))
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?
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;
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;
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
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
Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 6-29
What’s next?
• Strings• Structured data types• Pointers