0% found this document useful (0 votes)
12 views12 pages

Chapter 2 CS331 (28)

The document discusses various data types in programming, including primitive types like integers, floats, and booleans, as well as user-defined types such as records and arrays. It highlights design issues, operations, and the implementation of these data types across different programming languages. Additionally, it covers concepts like enumeration types, subrange types, and pointers, emphasizing their significance in programming language design.

Uploaded by

srajalrfy8
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)
12 views12 pages

Chapter 2 CS331 (28)

The document discusses various data types in programming, including primitive types like integers, floats, and booleans, as well as user-defined types such as records and arrays. It highlights design issues, operations, and the implementation of these data types across different programming languages. Additionally, it covers concepts like enumeration types, subrange types, and pointers, emphasizing their significance in programming language design.

Uploaded by

srajalrfy8
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/ 12

Data types ( Ύ Ύ Ύ ϴ ϴ )

A data type defines a collection of data objects and a set of predefined


operations on those objects.
ㄠ hose bjects j ctts ㄠjcc s j jsts 䁢 香 ㄠjcc j jsts jj bje⸳
䁢 香ts
A descriptor is the collection of the attributes of a variable
( j䁟m cm ㄠjcc j Rss)
An object represents an instance of a user-defined (abstract data) type
( jpmects so y tcts ( b c j j ) j䁢t j 香ts c⸳)
One design issue for all data types: What operations are defined and how
are they specified?
( j⸳je m⸳ j香s bjects j ctts : j jsts sjj jcct j ss jcm 香Ϝ )
Primitive Data Types ( a ϴ Ύ Ύ Ύ ϴ ϴ )
Almost all programming languages provide a set of primitive data types.
( j sjsts j jsts sjj ㄠjcc hs⸳ o c sts 䁟t jc yj )
Integer ( ‫׆‬m )
There may be as many as eight different integer types in a language
( 䁟 ts y 댳 mp jeR bjㄠ sjj jj ca tn m⸳ 䁢 )
Java’s signed integer sizes: byte, short, int, long
Float ( )
Languages for scientific use support at least two floating-point types
float and double.
( e香ts bjtts o‫׆‬s ㄠ jㄠjj c tts sjpmm t 䁟 ts ㄠb)
Decimal ( o )
• For business applications (money)
– Essential to COBOL ㈠s L
– C# offers a decimal data type
• Store a fixed number of decimal digits
• Advantage: accuracy( ojts)
1
• Disadvantages: limited range, wastes memory
bsje js 香s ts jo⸳
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
o 香 e
" o" t o ss " sjR" t c j ⸳ m䁢ㄠ : jots j
⸳ sts sj s j ‫׆‬s j 香 y 香ts sj s 香o ㄠ j댳䁢 香c⸳
s ots tjϮm : 쳌j
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 Unicode
o㈠s ⴠj ㄠ 쳌p⸳
ASCII :쳌j mts sjpmms 香‫׆‬s
Unicode : 16 쳌j ⸳j
jtjs ts 䁟 ts 錰t jmpo com⸳
y y R‫׆‬s y jpme
h ⸳ JavaScript s # C ㄠj⸳
Unicode o
String ( 㤵 ㌱ ‹ ‹㔰)
• 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?

2
• Typical operations:
– Assignment and copying( ejs b 䁢ms)
– Comparison (=, >, etc.) j㈠ o
– Catenation jm䁢mts ㄠ
• C and C++
– Not primitive tss jj jt
– Use char arrays and a library of functions that provide
operations
• SNOBOL4 (a string manipulation language)
– Primitive
– Many operations, including elaborate pattern matching
• Java
– Primitive via the String class
Character 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 options
Character String Type Evaluation Ϙi ϴ
• 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?
Character String Implementation pτi ϴ
• Static length: compile-time descriptor
• Limited dynamic length: may need a run-time descriptor for length
(but not in C and C++)

3
• Dynamic length: need run-time descriptor; allocation/de-allocation
is the biggest implementation problem
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
• Examples of primitive ordinal types in Java
• integer
• char
• boolean
Enumeration Types
All possible values, which are named constants, are provided in the
definition( ⸳ tmts y sj ts ce mtss 䁢香ccts jots jc jyj )
• C# example
enum days {mon, tue, wed, thu, fri, sat, sun};
• constant checked
Evaluation 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 types
Subrange Types
• An ordered contiguous subsequence of an ordinal type
– Example: 12..18 is a subrange of integer type
• Ada’s design
type Days is (mon, tue, wed, thu, fri, sat, sun);

4
subtype Weekdays is Days range mon..fri;
subtype Index is Integer range 1..100;
Day1: Days;
Day2: Weekday;
Day2 := Day1;
Subrange 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
specified range is detected as an error
Array Types
An array is an aggregate of homogeneous data elements in which an
individual element is identified by its position in the aggregate, relative to
the first element.
b y m䁢ㄠ j⸳je Ϯjy m⸳ mts ej cmcts j jsts R 䁢ㄠ ㄠjcc ㄠ ㈠ sㄠ yj댳mcts
. s‫׆‬s m䁢t t se䁢t ㄠjcccts y tLj o
Array Design Issues
What types are legal for subscripts? j䁢ts
Are subscripting expressions in element references range checked? ey
When are subscript ranges bound? jcts
When does allocation take place? ojcts
What is the maximum number of subscripts? cjo mos
Can array objects be initialized? j sjm Ϊs jots
Are any kind of slices allowed? s o
Array Indexing
• Indexing (or subscripting) is a mapping from indices to elements
array_name (index_value_list)  an element

5
• Index Syntax
– FORTRAN, PL/I, 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
Subscript Binding and Array Categories
• Stack-dynamic: subscript ranges are dynamically bound and the
storage allocation is dynamic (done at run-time)
– Advantage: flexibility (the size of an array need not be
known until the array is to be used)
( yj댳mcts sjpmms m⸳ m yj댳mcts c y t 쳌 ⸳ Ϊ) js cts : 쳌jcts
• Fixed heap-dynamic: similar to fixed stack-dynamic: storage
binding is dynamic but fixed after allocation (i.e., binding is done
when requested and storage is allocated from heap, not stack)
m⸳s ts j䁢ㄠ ts m⸳ ) jmpmts jt a 香ts 香j 䁢⸳b j ⸳쳌pmts ㈠
( j香 jts moϗcts 香s ts j香 ⸳쳌pmts jmp
• Heap-dynamic: binding of subscript ranges and storage allocation
is dynamic and can change any number of times
– Advantage: flexibility (arrays can grow or shrink during
program execution)
香c⸳s 香j 䁢⸳b ⸳쳌pmts jmp s j䁟mts s ets o j ㈠ : j香j 䁢⸳jts j香ts
s cts bjㄠ j䁟m⸳
( j sts j댳䁢 䁢a o s 댳mts jc䁢 香c⸳) js cts : 쳌j
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++
6
char *names [] = {“Bob”, “Jake”, “Joe”];
– Java initialization of String objects
String[] names = {“Bob”, “Jake”, “Joe”};
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)
• Ada allows array assignment but also catenation
• Fortran provides elemental operations because they are between
pairs of array elements
– For example, + operator between two arrays results in an
array of the sums of the element pairs of the two arrays
sjtts tn y L yj댳mctss Ϯcmc t yj댳mcts ct t j cㄠ jo APL yj
⸳b ‫׆‬s
(bjctts R 䁢ㄠ 香tt cts jsm ㄠ)
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
bjㄠ 댳j ㄠ j댳mts jc Ϯjy jme b t ‫׆‬s bjtm yj댳m j mects yj댳mcts
R 䁢tts bjㄠ 댳j Ϯt jcㄠ‫׆‬s 香s R 䁢tts
R 䁢tts s 댳m bjㄠ j댳R Ϯt 䁢Ϝo yj댳m
yj댳mct yj댳mc香 t댳t b t ‫׆‬s bjtm yj댳mcts Ϯ錰 j䁢ㄠ 香c

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
7
Slice Examples
Fortran 95
Integer, Dimension (10) :: Vector
Integer, Dimension (3, 3) :: Mat
Integer, Dimension (3, 3) :: Cube
Vector (3:6) is a four element array

Accessing Multi-dimensioned Arrays


• Two common ways:
– Row major order (by rows) – used in most languages
– column major order (by columns) – used in Fortran

8
Single-dime

9
Multi-dimen

Record Types
• A record is a possibly heterogeneous aggregate of data elements in
which the individual elements are identified by names
R 䁢tts j⸳je 香c⸳ j jsts R 䁢ㄠ ej cm ㄠjcc ㄠ ㈠ sㄠ cets jt⸳
cm‫׆‬s o Ϯjy ⸳b 댳ts
Definition of Records
• COBOL uses level numbers to show nested records; others use
recursive definition
• Record Field References
1. COBOL
field_name OF record_name_1 OF ... OF record_name_n
01 EMP-REC.
02 EMP-NAME.
05 FIRST PIC X(20).
05 MID PIC X(10).
05 LAST PIC X(20).
02 HOURLY-RATE PIC 99V99.
2. Others (dot notation)
record_name_1.record_name_2. ... record_name_n.field_name
type Emp_Rec_Type is record
First: String (1..20);
Mid: String (1..10);
Last: String (1..20);
Hourly_Rate: Float;
end record;
Emp_Rec: Emp_Rec_Type;
10
Most language use dot notation
Emp_Rec.Name

Pointer
• A pointer type variable has a range of values that consists of
memory addresses and a special value, nil
• Provide the power of indirect addressing
• Provide a way to manage dynamic memory
• A pointer can be used to access a location in the area where storage
is dynamically created (usually called a heap)
R o cjos 香s ts ⸳s 䁢ㄠ j香m mts jots ㄠjcc ㄠ oϗcts jj j䁟m jm

os jo jj䁢ㄠ jo jyj

11
j香j 䁢⸳jts 香s ts ㈠sb jms jyj
b ㄠ) jj 香Ϝ ⸳쳌pmts Ϝjn m⸳ j o 䁢cts y oj tn jRj t oϗ sjpmms 香c⸳
( moϗcts 香s ts j香 ce⸳

The assignment operation j = *ptr

12

You might also like