Programming Language(程式語言) Chapter 6
Data Types (資料型態)
Programming Language(程式語言) Chapter 6
Data Types (資料型態)
Topics
Introduction
Primitive data types (基本資料型態)
Character string types (符號字串型態)
User-defined ordinal types (使用者定義順序型態)
Array types (陣列型態)
Associative arrays (關聯陣列)
Record types (紀錄型態)
Union types (聯合型態)
Pointer and reference types (指標與參考型態)
Introduction
Data type defines
A collection of data objects and a set of predefined operations on those objects一群資料物件與再這物件上被預先定義運算集合
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沒有被定義再其他的資料型態中
Some primitive data types are merely (只是) reflections (反映) of the hardware
Integer type
Others require (需要) only a little non-hardware
Integer
The most common privative numeric (數字的) data type is integer
Java’s signed integer sizes
byte, short, int, long
C++ and C# include unsigned integer types
Unsigned types are often used for binary data
Floating Point
Model real numbers, but only as approximations (近似值)
Languages for scientific (科學) use support at least two floating-point types
Float and double
IEEE Floating-Point
Standard 754
IEEE Standard 754 Floating-Point
(單精確度) (倍精確度、雙精確度)
Decimal (10進位)
For business applications (money)
Essential (基本) to COBOL
C# offers a decimal data type
Store a fixed number of decimal digits
Advantage
Accuracy (準確)
Disadvantages
Limited range (有限的範圍)
Wastes memory (消耗記憶體)
Boolean
Simplest (最簡單) of all
Range of values: two elements
True
False
Could be implemented as bits, but often as bytes
Advantage
Readability
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
Character String Types
Values are sequences of characters
Design issues
Should strings be simply a special kind of character array or a primitive type?
Should strings have static or dynamic length?
C and C++
Not primitive (基本)
Use char arrays and a library of functions that provide operations
Java
Primitive via the String class
Character String Length Options
Static
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)
Perl, JavaScript
Ada supports all three string length options
Character String Implementation
Static length
Compile-time descriptor
Limited dynamic length
May need a run-time descriptor for length (but not in C and C++)
Dynamic length
Need run-time descriptor; allocation/de-allocation is the biggest implementation problem
Compile- and Run-Time Descriptors
Compile-time descriptor for static strings Run-time descriptor for limited dynamic strings
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
User-Defined Ordinal Types
In some language, users can define two kinds of ordinal type
Enumeration (列舉)
Subrange (子範圍)
Enumeration Types
All possible values, which are named (被命名) constants (常數), are provided in the definition
C# example
enum days {mon, tue, wed, thu, fri, sat, sun};
Design issues
Are enumeration values coerced (強制) to integer?
Any other type coerced to an enumeration type?
Subrange Types
An ordered contiguous (連續) subsequence of an ordinal type
Example: 12..18 is a subrange of integer type
Pascal, Ada
Ada’s design
type Days is (mon, tue, wed, thu, fri, sat, sun);
subtype Weekdays is Days range mon..fri;
subtype Index is Integer range 1..100;
Day1: Days;
Day2: Weekday;
Day2 := Day1;
Comments