0% found this document useful (0 votes)
13 views36 pages

Lecture 5

Chapter 5 covers data types in programming, defining a data type as a collection of data objects and operations. It discusses primitive data types, including integers, floats, booleans, and character strings, as well as user-defined types like enumerations and arrays. The chapter also addresses type checking, ensuring that operands are compatible with operators in programming languages.

Uploaded by

ENES
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views36 pages

Lecture 5

Chapter 5 covers data types in programming, defining a data type as a collection of data objects and operations. It discusses primitive data types, including integers, floats, booleans, and character strings, as well as user-defined types like enumerations and arrays. The chapter also addresses type checking, ensuring that operands are compatible with operators in programming languages.

Uploaded by

ENES
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 36

Chapter 5

Data Types
Tentative Course Outline

1. Preliminaries -Introduction to Programming Languages


2. Evolution of the Major Programming Languages
3. Describing Syntax and Semantics
4. Names, Bindings, and Scopes
5. Data Types
6. Expressions and Assignment Statements
7. Statement-Level Control Structures
8. Subprograms
9. Implementing Subprograms
10. Abstract Data Types and Encapsulation Concepts
11. Support for Object-Oriented Programming
12. Concurrency
13. Exception Handling and Event Handling
14. Functional Programming Languages
Chapter 5 Topics

• Introduction
• Primitive Data Types
• Character String Types
• Enumeration Types
• Array Types
• Associative Arrays
• Type Checking

Copyright © 2017 Pearson Education, Ltd. All rights reserved. 1-3


Introduction

• 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

Copyright © 2017 Pearson Education, Ltd. All rights reserved. 1-4


Introduction
• The collection of attributes of a variable typically refers
to the attributes (or fields) of an object, which define the
state of the object. These attributes are usually stored in
an object's namespace (its dictionary) and can be
accessed or modified directly.
• However, the concept of descriptors involves more
control over how these attributes are accessed, modified,
or deleted, enabling additional behavior to be introduced.

Copyright © 2017 Pearson Education, Ltd. All rights reserved. 1-5


Primitive 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

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

Copyright © 2017 Pearson Education, Ltd. All rights reserved. 1-6


Primitive Data Types: Integer

• There may be as many as eight different integer types in a language


• Java’s signed integer sizes: byte, short, int, long

Java’s Signed Integer Types


Java provides four signed integer types:

Copyright © 2017 Pearson Education, Ltd. All rights reserved. 1-7


Primitive Data Types: Floating Point
• Model real numbers, but only as approximations
• Languages for scientific use support at least two floating-point types (e.g., float and double
• IEEE Floating-Point Standard 754
• Floating-point numbers are useful for representing irrational numbers (like π), decimals (like
3.14), and scientific notation (like 1.2e5). However, they can suffer from precision loss due
to the way they are stored in memory.

Copyright © 2017 Pearson Education, Ltd. All rights reserved. 1-8


Primitive Data Types: Complex

• 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

Languages That Support Complex Numbers

Copyright © 2017 Pearson Education, Ltd. All rights reserved. 1-9


Slower Performance: The decimal data type generally works slower than
the float and double data types because it uses more memory and requires
more complex computations.
Memory Consumption: The decimal data type typically consumes more
Primitive Data Types: Decimal memory compared to double and float types. (16 bytes)

• The decimal data type is often used for handling precise


decimal arithmetic, especially in financial, scientific, and
engineering applications.
• It avoids some of the precision issues inherent in floating-
point numbers (like float or double) due to their binary
representation.

Decimal is a built-in data type in C# Many relational database management


systems use DECIMAL or NUMERIC data
types

Decimal is available in Python through the decimal module.


It provides a Decimal class

Java doesn't have a native decimal type, but


Decimal is a native type in VB.NET it offers BigDecimal as a class

Copyright © 2017 Pearson Education, Ltd. All rights reserved. 1-10


Primitive 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: readability
• It is used for logical operations, conditions,
and control flow in almost every
programming language.

Copyright © 2017 Pearson Education, Ltd. All rights reserved. 1-11


Primitive Data Types: Character

• Stored as numeric codings


• Most commonly used coding: ASCII
• An alternative, 16-bit coding: Unicode (UCS-2)
– Includes characters from most natural languages
– Originally used in Java
– C# and JavaScript also support Unicode
• 32-bit Unicode (UCS-4)
– Supported by Fortran, starting with 2003
• Character data types are stored as numeric codings, where each character is mapped to a
number.

Copyright © 2017 Pearson Education, Ltd. All rights reserved. 1-12


Primitive Data Types: Character
Javascript

Copyright © 2017 Pearson Education, Ltd. All rights reserved. 1-13


Character String Types

• Values are sequences of characters


• In most high-level languages, character strings are not primitive types because they are
considered objects, arrays, or complex types. However, single characters (like char in C or
Java) are usually primitive types.

Copyright © 2017 Pearson Education, Ltd. All rights reserved. 1-14


Character String Types Operations
• Typical operations:
– Assignment and copying
– Comparison (=, >, etc.)
– Catenation
– Substring reference
– Pattern matching s1 and s2 point to the same string object in memory.

Copyright © 2017 Pearson Education, Ltd. All rights reserved. 1-15


Character String Type in Certain Languages

• 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.

Copyright © 2017 Pearson Education, Ltd. All rights reserved. 1-16


Character String Length Options

• Static: COBOL, Java’s String class


• Limited Dynamic Length: C and C++
– In these languages, 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

Copyright © 2017 Pearson Education, Ltd. All rights reserved. 1-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
• 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

• The int type represents whole numbers in a range that can


be associated with positive integers.
• Example range: -2^31 to 2^31 - 1 (for 32-bit systems)
• Ordinal property: The integers have a natural order, and
operations like <, >, <=, >=, == are meaningful.
Copyright © 2017 Pearson Education, Ltd. All rights reserved. 1-18
• The char type represents single characters from the Unicode set, and its
values can be treated as numbers (based on their Unicode code points).
User-Defined Ordinal Types • Example: 'A' has a Unicode value of 65, and 'B' is 66, so we can compare
and perform arithmetic with characters just like integers.
• Ordinal property: The characters are ordered by their Unicode code point
values, allowing comparisons and arithmetic.

• The boolean type represents two values: true and false.


• While it seems non-ordinal, it can still be thought of as having an implicit ordinal
range: false = 0 and true = 1.
• Ordinal property: true can be seen as 1 and false as 0, so boolean values can be
compared in terms of their numerical equivalents.

Copyright © 2017 Pearson Education, Ltd. All rights reserved. 1-19


Enumeration Types
• An enumeration (enum) is a data type that consists of a set of named values called elements or members.
• Enumerations are used to represent a collection of related constants, making code more readable and
manageable.
• Instead of using arbitrary numbers or strings to represent certain values, you use descriptive names to make
the code more self-documenting and less prone to errors.

Copyright © 2017 Pearson Education, Ltd. All rights reserved. 1-20


Enumeration Types

Enum Class: Python provides the Enum base class to


define enumerations. Each member of the
enumeration has a name (e.g., WEDNESDAY) and a
value (e.g., 4).

Copyright © 2017 Pearson Education, Ltd. All rights reserved. 1-21


Array Types

• An array is a homogeneous aggregate of data elements in which an individual element is


identified by its position in the aggregate, relative to the first element.

Array Design Issues

• What types are legal for subscripts?


• Are subscripting expressions in element references range checked?
• When are subscript ranges bound?
• When does allocation take place?
• Are ragged or rectangular multidimensional arrays allowed, or both?
• What is the maximum number of subscripts?
• Can array objects be initialized?
• Are any kind of slices supported?

Copyright © 2017 Pearson Education, Ltd. All rights reserved. 1-22


Array Types

Array Indexing

• Indexing (or subscripting) is a mapping from indices to elements


array_name (index_value_list) → an element
• Index Syntax
– Fortran and Ada use parentheses
• Ada explicitly uses parentheses to show uniformity between array references and
function calls because both are mappings
– Most other languages use brackets

Copyright © 2017 Pearson Education, Ltd. All rights reserved. 1-23


Array Types

Array Initialization

• Some language allow initialization at the time of storage allocation


– C, C++, Java, C# example
int list [] = {4, 5, 7, 83}
– Character strings in C and C++
char name [] = ″freddie″;
– Arrays of strings in C and C++
char *names [] = {″Bob″, ″Jake″, ″Joe″];
– Java initialization of String objects
String[] names = {″Bob″, ″Jake″, ″Joe″};

Copyright © 2017 Pearson Education, Ltd. All rights reserved. 1-24


Array Types
Heterogeneous Arrays
• A heterogeneous array is one in which the elements need not be of the same type
• Supported by Perl, Python, JavaScript, and Ruby

Copyright © 2017 Pearson Education, Ltd. All rights reserved. 1-25


Array Types
Arrays Operations

• 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

Copyright © 2017 Pearson Education, Ltd. All rights reserved. 1-26


Array Types
Arrays Operations

Copyright © 2017 Pearson Education, Ltd. All rights reserved. 1-27


Array Types
Rectangular and Jagged Arrays
• A rectangular array is a multi-dimensioned array in which all of the rows have the same
number of elements and all columns have the same number of elements
• A jagged matrix has rows with varying number of elements
– Possible when multi-dimensioned arrays actually appear as arrays of arrays
The jagged array has 3 rows, but the number of
columns in each row varies:
Row 1: 3 elements
Row 2: 2 elements
Row 3: 4 elements

Here, the array has 3 rows and 4


columns, and each row contains
exactly 4 elements.

Copyright © 2017 Pearson Education, Ltd. All rights reserved. 1-28


Array Types

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

vector (3:6) is a three-element array


mat[0][0:2] is the first and second element of the first row of mat

• Ruby supports slices with the slice method


list.slice(2, 2) returns the third and fourth elements of list

Copyright © 2017 Pearson Education, Ltd. All rights reserved. 1-29


Array Types

Accessing Multi-dimensioned Arrays

• Two common ways:


– Row major order (by rows) – used in most languages
– Column major order (by columns) – used in Fortran
– A compile-time descriptor for a multidimensional array

Example:
347
6 2 5 → row major order (3,4,7,6,2,…)
138 column major order (3,6,1,4,2,…)

Copyright © 2017 Pearson Education, Ltd. All rights reserved. 1-30


Associative Arrays

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?

Built-in type in Perl, Python, Ruby, and Lua


– In Lua, they are supported by tables

• Names begin with %; literals are delimited by parentheses


%hi_temps = ("Mon" => 77, "Tue" => 79, "Wed" => 65, …);
• Subscripting is done using braces and keys
$hi_temps{"Wed"} = 83;
– Elements can be removed with delete
delete $hi_temps{"Tue"};
Associative Arrays in Perl
Copyright © 2017 Pearson Education, Ltd. All rights reserved. 1-31
Type Checking

• 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.

• A type error is the application of an operator to an operand of an inappropriate type

Copyright © 2017 Pearson Education, Ltd. All rights reserved. 1-32


Type Checking

• 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.

Copyright © 2017 Pearson Education, Ltd. All rights reserved. 1-33


Type Checking

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

Python does not automatically convert "10" (string) to an integer. You


must explicitly convert the string to an integer

Copyright © 2017 Pearson Education, Ltd. All rights reserved. 1-34


Type Checking
Strong typing

Copyright © 2017 Pearson Education, Ltd. All rights reserved. 1-35


Type Checking
Weak typing
JavaScript implicitly converts
Subtraction forces x into a number

PHP also allows implicit type


conversions

Copyright © 2017 Pearson Education, Ltd. All rights reserved. 1-36

You might also like