Unformatted text preview:

ISBN 0-321-33025-0Chapter 6Chapter 6Chapter 6Chapter 6Data TypesCopyright © 2006 Addison-Wesley. All rights reserved. 1-2Chapter 6 Topics• Introduction• Primitive Data Types• Character String Types• User-Defined Ordinal Types• Array Types• Associative Arrays• Record Types• Union Types• Pointer and Reference TypesCopyright © 2006 Addison-Wesley. All rights reserved. 1-3Introduction• A data typedefines a collection of data objects and a set of predefined operations on those objects• A descriptoris the collection of the attributes of a variable• An objectrepresents an instance of a user-defined (abstract data) type• One design issue for all data types: What operations are defined and how are they specified?Copyright © 2006 Addison-Wesley. All rights reserved. 1-4Primitive Data Types• Almost all programming languages provide a set of primitive data types• Primitive data types: Those not defined in terms of other data types• Some primitive data types are merely reflections of the hardware• Others require little non-hardware supportCopyright © 2006 Addison-Wesley. All rights reserved. 1-5Primitive Data Types: Integer• 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’s signed integer sizes: byte, short, int, longCopyright © 2006 Addison-Wesley. All rights reserved. 1-6Primitive Data Types: Floating Point• Model real numbers, but only as approximations• Languages for scientific use support at least two floating-point types (e.g., floatand double; sometimes more• Usually exactly like the hardware, but not always• IEEE Floating-PointStandard 754Copyright © 2006 Addison-Wesley. All rights reserved. 1-7Primitive Data Types: Decimal• For business applications (money)– Essential to COBOL– C# offers a decimal data type• Store a fixed number of decimal digits •Advantage: accuracy•Disadvantages: limited range, wastes memoryCopyright © 2006 Addison-Wesley. All rights reserved. 1-8Primitive Data Types: Boolean• Simplest of all• Range of values: two elements, one for “true” and one for “false”• Could be implemented as bits, but often as bytes– Advantage: readabilityCopyright © 2006 Addison-Wesley. All rights reserved. 1-9Primitive Data Types: Character• Stored as numeric codings• Most commonly used coding: ASCII• An alternative, 16-bit coding: Unicode– Includes characters from most natural languages– Originally used in Java– C# and JavaScript also support UnicodeCopyright © 2006 Addison-Wesley. All rights reserved. 1-10Character String Types • Values are sequences of characters• Design issues:– Is it a primitive type or just a special kind of array?– Should the length of strings be static or dynamic?Copyright © 2006 Addison-Wesley. All rights reserved. 1-11Character String Types Operations• Typical operations:– Assignment and copying– Comparison (=, >, etc.) – Catenation– Substring reference– Pattern matchingCopyright © 2006 Addison-Wesley. All rights reserved. 1-12Character String Type in Certain Languages• C and C++– Not primitive– Use chararrays and a library of functions that provide operations• SNOBOL4 (a string manipulation language)– Primitive– Many operations, including elaborate pattern matching• Java– Primitive via the StringclassCopyright © 2006 Addison-Wesley. All rights reserved. 1-13Character String Length Options• Static: COBOL, Java’s String class•Limited Dynamic Length: C and C++– In C-based language, a special character is used to indicate the end of a string’s characters, rather than maintaining the length•Dynamic(no maximum): SNOBOL4, Perl, JavaScript• Ada supports all three string length optionsCopyright © 2006 Addison-Wesley. All rights reserved. 1-14Character String Type Evaluation• Aid to writability• As a primitive type with static length, they are inexpensive to provide--why not have them?• Dynamic length is nice, but is it worth the expense?Copyright © 2006 Addison-Wesley. All rights reserved. 1-15Character String Implementation• Static length: compile-time descriptor• Limited dynamic length: may need a run-time descriptor for length (but not in C and C++)• Dynamic length: need run-time descriptor; allocation/de-allocation is the biggest implementation problemCopyright © 2006 Addison-Wesley. All rights reserved. 1-16Compile- and Run-Time DescriptorsCompile-time descriptor for static stringsRun-time descriptor for limited dynamic stringsCopyright © 2006 Addison-Wesley. All rights reserved. 1-17User-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• Examples of primitive ordinal types in Java– integer– char– booleanCopyright © 2006 Addison-Wesley. All rights reserved. 1-18Enumeration Types• All possible values, which are named constants, are provided in the definition• C# exampleenum days {mon, tue, wed, thu, fri, sat, sun};• Design issues– Is an enumeration constant allowed to appear in more than one type definition, and if so, how is the type of an occurrence of that constant checked?– Are enumeration values coerced to integer?– Any other type coerced to an enumeration type?Copyright © 2006 Addison-Wesley. All rights reserved. 1-19Evaluation of Enumerated Type• Aid to readability, e.g., no need to code a color as a number• Aid to reliability, e.g., compiler can check: – operations (don’t allow colors to be added) – No enumeration variable can be assigned a value outside its defined range– Ada, C#, and Java 5.0 provide better support for enumeration than C++ because enumeration type variables in these languages are not coerced into integer typesCopyright © 2006 Addison-Wesley. All rights reserved. 1-20Subrange Types• An ordered contiguous subsequence of an ordinal type– Example: 12..18 is a subrange of integer type• Ada’s designtype Days is (mon, tue, wed, thu, fri, sat, sun);subtype Weekdays is Days range mon..fri;subtype Index is Integer range 1..100;Day1: Days;Day2: Weekday;Day2 := Day1;Copyright © 2006 Addison-Wesley. All rights reserved. 1-21Subrange Evaluation• Aid to readability– Make it clear to the readers that variables of subrange can store only certain range of values• Reliability– Assigning a value to a subrange variable that is outside the


View Full Document

DePaul CSC 447 - Data Types

Download Data Types
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Data Types and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Data Types 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?