Chapter 4 - Data Types
Chapter 4 - Data Types
2
Elementary Data Types:
1. PRIMITIVE DATA TYPES: Data types which are not defined in
terms of other types.
◦ Boolean: Represents true or false values. It's often used for conditions
and logical operations.
◦ Integer : Represents whole numbers, positive or negative, without
fractional parts. Different programming languages provide various sizes
of integer types (e.g., int, long, short).
◦ Floating -Point : Represents real numbers with fractional parts. It
includes single-precision (float) and double-precision (double) types.
◦ Character : Represents individual characters, often using ASCII or
Unicode encoding. Examples include char in C/C++ and char in Java.
◦ String : Represents sequences of characters. Strings can vary in length
and are often used for text manipulation.
◦ Enumeration (Enum ): Defines a set of named integer constants.
Enums are used to represent a set of distinct values.
3
y Data types which are not defined in terms of other
types.
1. Numeric types
Integers
x byte, short, int and long
x signed and unsigned
Integer representation
x Sign-magnitude
x A negative integer could be stored in sign-
magnitude notation, in which the sign bit is set
to indicate a negative and the remainder of the
bit string represents the absolute value of the
number.
5
y Ones-complement
◦ In ones-complement notation, the negative of an
integer is stored as the logical complement of its
absolute value
y Twos-complement
◦ In twos-compIement notation, the representation of a
negative integer is formed by taking the logical
complement of the positive version of the number and
adding one.
6
y Example: Sign-magnitude
57 could be represented by 00111001
-57 could be represented by 10111001
y Example: Ones-complement
57 is represented by 00111001
-57 is represented by 11000110
y Example: Twos-complement
57 is represented by 00111001
-57 is represented by 11000110
+ 1
11000111 7
y Floating-points
y Computer implementation of real numbers
y Floating point numbers are commonly represented as a
sign, a significand, or fractional part, and an exponent.
y The IEEE has defined formats for 32 bit and 64 bit
floating point implementations
y Most computers and computer languages support
these IEEE 754 implementations.
8
PRIMITIVE DATA TYPES
y Decimal
◦ Decimal types are stored using binary codes for the
decimal digits.
◦ These representations are called binary coded decimal
(BCD)
◦ In some cases, they are stored one digit per byte, but
in others they are packed two digits per byte
◦ Either way, they take more storage than binary
representations.
9
PRIMITIVE DATA TYPES
y Character types
◦ Character data are stored in computers as numeric codings
◦ Traditionally, the most commonly used coding was the 8-bit
code ASCII (American Standard Code for Information
Interchange), which uses the values 0 to 127 to code 128
different characters.
◦ Because of the globalization of business and the need for
computers to communicate with other computers around
the world, The ASCII character set is rapidly becoming
inadequate.
◦ A I6-bit character set named Unicode has been developed
as an alternative
x Example: char *str = “computer”;
x char country[50]; country=“Gini”;
country=“Ethiopia”;
11
ARRAY TYPES
y 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.
y References to individual array elements are specified using
subscript expressions
y Some design issues
◦ What type is legal for subscript or index?
◦ Are subscripting expressions in element references range
checked?
◦ When are array allocations take place?
◦ Are ragged or rectangular multidimensioned arrays
allowed, or both?
◦ Can arrays be initialized when they have their storage
allocated?
12
ARRAY TYPES
y Arrays and Indices
◦ Specific elements of an array are referenced by means of a two-
level syntactic mechanism: aggregate name and index.
◦ Syntax: The array name is followed by the list of subscripts,
which is surrounded by either parentheses or brackets.
◦ In some PLs, array indices were enclosed in parentheses
problem of identifying array element accessing and subprogram
calling.
◦ Consider the following Ada assignment statement
13
RECORD TYPES
y A record is an aggregate of data elements in which the
individual elements are identified by names and accessed
through offsets from the beginning of the structure.
y Used to model a collection of data in which the individual
elements are not of the same type or size
y Example: Information about a college student might include
name, student number, grade point average, and so forth
x A data type for such a collection might use a character
string for the name, an integer for the student number, a
floating point for the grade point average, and so forth
y In C, C++, and C#, records are supported with the struct
data type
14
EXERCISES
17
18
Chapter Four
Fundamental Object
oriented programming
concepts
19
Fundamental oop concepts
y Object; a thing from real word, a thing that you want to store
and process data about , an entity which has properties and task
to be performed eg book, person , #person(name, sex, age,
salary, profession)
y Class: template to create object, a code written by programmer,
consists a number of objects.
1. Abstraction in programming refers to the process of simplifying
complex systems by breaking them down into smaller, more
manageable components while hiding unnecessary details.
◦ Abstraction enables programmers to focus on essential
aspects of a system or data structure, making code more
understandable and maintainable.
◦ abstraction is simplification of facts
◦ : Consider a personnel file of an employer. This set may
include Name, Age, Salary but it will most probably not include
irrelevant data such as the hair color, weight, and height.
24