Lecture 5
Lecture 5
Data Types
Tentative Course Outline
• Introduction
• Primitive Data Types
• Character String Types
• Enumeration Types
• Array Types
• Associative Arrays
• Type Checking
• A data type defines a collection of data objects and a set of predefined operations on those
objects
• A descriptor is the collection of the attributes of a variable
• An object represents an instance of a user-defined (abstract data) type
• Primitive data types like integers, floats, and booleans define a specific kind of value and
operations (e.g., arithmetic operations on integers or comparisons on booleans).
• Abstract data types (ADTs) like stacks, queues, or lists define both the structure of the
data and the allowed operations (e.g., pushing or popping elements from a stack, enqueuing
or dequeuing from a queue).
• Some languages support a complex type, e.g., C99, Fortran, and Python
• Each value consists of two floats, the real part and the imaginary part
• Literal form (in Python): (7 + 3j), where 7 is the real part and 3 is the imaginary part
• This is especially useful in scientific computing, engineering, and signal processing
• 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: readability
• It is used for logical operations, conditions,
and control flow in almost every
programming language.
• C and C++
– Not primitive
– Use char arrays and a library of functions that provide operations
• SNOBOL4 (a string manipulation language)
– Primitive
– Many operations, including elaborate pattern matching
• Fortran and Python
– Primitive type with assignment and several operations
• Java
– Primitive via the String class
• Perl, JavaScript, Ruby, and PHP
- Provide built-in pattern matching, using regular expressions
• In Python, strings are immutable objects, meaning once they are created, their value cannot
be changed. They are implemented as objects of the str class in Python's object-oriented
system.
• An ordinal type is one in which the range of possible values can be easily associated with the
set of positive integers
• An ordinal type refers to a data type where its possible values can be associated with a
sequence of integers, typically starting from 0 or 1. The values of an ordinal type are
usually ordered, meaning that each value has a specific position in the range
• Examples of primitive ordinal types in Java
– integer
– char
– boolean
Array Indexing
Array Initialization
• APL provides the most powerful array processing operations for vectors and matrixes as well
as unary operators (for example, to reverse column elements)
• Python’s array assignments, but they are only reference changes.
• Python also supports array catenation and element membership operations
• Ruby also provides array catenation
Slices
• A slice is some substructure of an array; nothing more than a referencing mechanism
• Slices are only useful in languages that have array operations
• Python
vector = [2, 4, 6, 8, 10, 12, 14, 16]
mat = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
Example:
347
6 2 5 → row major order (3,4,7,6,2,…)
138 column major order (3,6,1,4,2,…)
An associative array is an unordered collection of data elements that are indexed by an equal
number of values called keys
– User-defined keys must be stored
Design issues:
- What is the form of references to elements?
- Is the size static or dynamic?
• Generalize the concept of operands and operators to include subprograms and assignments
• Type checking is the activity of ensuring that the operands of an operator are of compatible
types
• A compatible type is one that is either legal for the operator, or is allowed under language
rules to be implicitly converted, by compiler- generated code, to a legal type
– This automatic conversion is called a coercion.
• Coercion refers to the implicit conversion of one type to another, performed by the compiler
or interpreter.
• This occurs automatically if a type conversion is allowed by the language's rules.
• Example: In many languages, adding an integer to a float automatically coerces the integer to
a float before performing the addition. The result is a float.
Strong typing
• Strong typing means that a programming language enforces strict rules about how data types
interact.
• A strongly typed language does not implicitly convert types in ways that could lead to
unexpected behavior.
• If a conversion is needed, it must be done explicitly by the programmer.
• Strong typing languages enforce strict type rules (e.g., Python, Java, Rust).
• Weak typing languages allow implicit conversions (e.g., JavaScript, PHP).