CHAPTER 6 - Data Types

0.0(0)
Studied by 0 people
call kaiCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/92

encourage image

There's no tags or description

Looks like no tags are added yet.

Last updated 8:23 PM on 5/20/26
Name
Mastery
Learn
Test
Matching
Spaced
Call with Kai

No analytics yet

Send a link to your students to track their progress

93 Terms

1
New cards

Data Type

defines a collection of data values and a set of predefined operations on those values

2
New cards

Descriptor

the collection of the attributes of a variable -- used for type checking and by allocation/deallocation operations

3
New cards

Descriptor -- static attributes

required only at compile time -- built by the compiler as part of the symbol table

4
New cards

Descriptor -- dynamic attributes

part or all of the descriptor must be maintained during execution

5
New cards

Primitive Data Types

those not defined in terms of other data types

6
New cards

4 numeric types

Integer -- Floating-point -- Complex -- Decimal

7
New cards

Integer

the most common primitive numeric data type

8
New cards

Java's 4 signed integer sizes

byte -- short -- int -- long

9
New cards

Floating-point

models real numbers but only as approximations -- stored in binary on most computers

10
New cards

2 properties defining floating-point values

Precision (accuracy of fractional part, measured in bits) -- Range (range of fractions and exponents)

11
New cards

Complex

each value consists of two floats: real part and imaginary part -- supported by Fortran and Python -- Python literal form: (7 + 3j)

12
New cards

Decimal

stores a fixed number of decimal digits with decimal point at a fixed position -- primary data type for business processing -- essential to COBOL

13
New cards

Advantage of Decimal type

accuracy of decimal values

14
New cards

2 disadvantages of Decimal type

limited range since no exponents are allowed -- representation wastes memory

15
New cards

Boolean

introduced by ALGOL 60 -- represents switches and flags -- two values: true and false -- enhances readability

16
New cards

Boolean in C89

no Boolean type -- uses int -- nonzero is true -- zero is false

17
New cards

Character types

stored as numeric codings -- ASCII (8-bit) or Unicode (16-bit)

18
New cards

ASCII

American Standard Code for Information Interchange -- 8-bit code -- traditionally most commonly used

19
New cards

Unicode (UCS-2)

16-bit character coding -- first widely used in Java

20
New cards

UCS-4 / UTF-32

4-byte character code developed by Unicode Consortium and ISO after 1991 -- described in ISO/IEC 10646 Standard published in 2000

21
New cards

Character String Type

values are sequences of characters

22
New cards

2 design issues for Character String Types

Is it a primitive type or a special kind of array? -- Is the length of objects static or dynamic?

23
New cards

5 typical string operations

Assignment -- Comparison -- Catenation -- Substring Reference -- Pattern Matching

24
New cards

C/C++ string library functions

strcpy (copy) -- strcat (catenate) -- strcmp (lexicographic compare) -- strlen (length excluding null)

25
New cards

C strings

terminated with null character represented by zero -- implemented as arrays of char

26
New cards

Java strings

String class (constant/immutable) -- StringBuffer class (changeable)

27
New cards

Python strings

immutable -- similar to Java's String class

28
New cards

3 string length options

Static Length -- Limited Dynamic Length -- Dynamic Length

29
New cards

Static Length String

length set when string is created -- immutable -- Java String class, C++ standard library, .NET (C#, F#)

30
New cards

Limited Dynamic Length String

varying length up to a declared fixed maximum -- example: C

31
New cards

Dynamic Length String

no maximum length -- requires dynamic storage allocation/deallocation overhead -- examples: Perl, JavaScript

32
New cards

Static Length String descriptor

compile-time -- 3 fields: name of type, type's length, address of first char

33
New cards

Limited Dynamic Length String descriptor

run-time -- stores fixed maximum length and current length

34
New cards

Dynamic Length String descriptor

run-time -- stores only current length -- allocation/deallocation is biggest implementation problem

35
New cards

3 approaches to dynamic string storage

linked list (extra storage, pointer chasing slows operations) -- arrays of pointers to heap characters (faster than linked list but extra memory) -- adjacent storage cells (slower alloc/dealloc but faster operations and less storage -- typically used)

36
New cards

Enumeration Type

all possible values, which are named constants, are enumerated in the definition -- implicitly assigned integer values 0, 1, ...

37
New cards

Enumeration constants

implicitly assigned 0, 1, ... but can be explicitly assigned any integer literal

38
New cards

Enumeration in Java

added in Java 5.0 -- all enum types are implicitly subclasses of predefined class Enum

39
New cards

Languages without enumeration types

Perl -- JavaScript -- PHP -- Python -- Ruby -- Lua

40
New cards

Array

a homogeneous aggregate of data elements -- individual element identified by its position relative to the first element

41
New cards

Indexing/Subscripting (array)

a mapping from indices to elements

42
New cards

Languages without array range checking

C -- C++ -- Perl -- Fortran

43
New cards

Languages with array range checking

Java -- ML -- C#

44
New cards

Ada array subscript notation

uses parentheses () -- reduces readability because () also used for subprogram parameters

45
New cards

C-based array subscript notation

uses square brackets []

46
New cards

2 distinct types in an array type

element type -- type of the subscripts

47
New cards

Associative Array

an unordered collection of data elements indexed by keys -- each element is a key-value pair

48
New cards

Associative Array in Perl

called hashes -- names begin with %

49
New cards

Associative Array in Python

called dictionaries

50
New cards

Lua Table

an associative array where both keys and values can be any type

51
New cards

Record

a possibly heterogeneous aggregate of data elements where individual elements are identified by names -- implemented as struct in C, C++, C#

52
New cards

Difference between record and array

record elements (fields) are not referenced by indices -- fields are named with identifiers

53
New cards

Tuple

a data type similar to a record except elements are not named

54
New cards

Python Tuple

immutable -- mixed types allowed -- indexing begins at 1 -- catenated with + -- deleted with del

55
New cards

ML/F# Tuples

used to allow functions to return multiple values

56
New cards

List (Scheme/Common Lisp)

delimited by parentheses -- elements not separated by punctuation -- data and code have the same form

57
New cards

Scheme list operation -- CAR

returns the first element of its list parameter

58
New cards

Scheme list operation -- CDR

returns the remainder of the list after the first element is removed

59
New cards

Scheme list operation -- CONS

puts its first parameter into its second parameter (a list) to make a new list

60
New cards

Scheme list operation -- LIST

returns a new list of its parameters

61
New cards

ML list operations

hd (head -- equivalent to CAR) -- tl (tail -- equivalent to CDR) -- :: for CONS -- elements in brackets separated by commas -- must be same type

62
New cards

Python List

mutable -- heterogeneous -- serves as Python's arrays -- indices begin at zero

63
New cards

Union

a type whose variables are allowed to store different type values at different times during execution

64
New cards

F# Union

declared with type statement using OR operators (|) -- accessed via pattern-matching structure

65
New cards

Pointer Type

variables have a range of values consisting of memory addresses and a special value nil

66
New cards

nil (pointer)

not a valid address -- indicates pointer cannot currently reference any memory cell

67
New cards

2 uses of pointers

provide indirect addressing -- manage dynamic memory (heap access)

68
New cards

2 fundamental pointer operations

assignment -- dereferencing

69
New cards

Dereferencing (C++)

explicitly specified with () as a prefix unary operator -- p -> age is equivalent to (p).age

70
New cards

C/C++ pointer operators

* for dereferencing -- & for address-of

71
New cards

Dangling Pointer

a pointer that points to a heap-dynamic variable that has been deallocated

72
New cards

4 reasons dangling pointers are dangerous

location may be reallocated to a new variable -- type checks become invalid -- new value has no relation to old dereferenced value -- storage manager may fail

73
New cards

C/C++ pointers

extremely flexible but dangerous -- can point anywhere in memory -- pointer arithmetic is possible

74
New cards

Type Checking

ensuring the operands of an operator are of compatible types

75
New cards

Compatible Type

legal for the operator OR allowed to be implicitly converted by compiler-generated code

76
New cards

Coercion

automatic/implicit type conversion -- example: int coerced to float when added to float in Java

77
New cards

Type Error

application of an operator to an operand of an inappropriate type

78
New cards

Static type bindings

nearly all type checking can be done at compile time

79
New cards

Dynamic type bindings

type checking must be done at run time

80
New cards

Languages with only dynamic type checking

JavaScript -- PHP

81
New cards

Strong Typing

a language is strongly typed if type errors are always detected -- types of all operands determinable at compile or run time

82
New cards

Strongly typed languages

Java -- C#

83
New cards

NOT strongly typed languages

C -- C++ (because union types are not type checked)

84
New cards

Advantage of strong typing

allows detection of misuses of variables that result in type errors

85
New cards

Coercion weakens strong typing

coercion results in loss of error detection -- C/C++ less reliable than ML/F# which have no coercions

86
New cards

Type Equivalence

two types are equivalent if an operand of one type can be substituted for the other without coercion

87
New cards

2 approaches to type equivalence

Name Type Equivalence -- Structure Type Equivalence

88
New cards

Name Type Equivalence

two variables have equivalent types if in the same declaration or using the same type name -- easy to implement but highly restrictive

89
New cards

Structure Type Equivalence

two variables have equivalent types if their types have identical structures -- more flexible but harder to implement

90
New cards

C type equivalence

uses both -- name equivalence for struct, enum, and union -- structure equivalence for other nonscalar types

91
New cards

Type Theory

a broad area of study in mathematics, logic, computer science, and philosophy

92
New cards

2 branches of Type Theory in CS

Practical (data types in commercial PLs) -- Abstract (typed lambda calculus)

93
New cards

Type System

a set of types and the rules that govern their use in programs