Data Types
Created time | |
---|---|
Tags | A2C16Notes |
What does it mean by data type
- Data type refers to the type of value a variable has and what type of mathematical, relational or logical operations can be applied without causing an error.(value, operation)
Primitive data types VS non-primitive data types
Primitive data types: The data types that are supported by computer hardware
- Common primitive data types
- integer
- floating -point numbers
- boolean
- character
Non-primitive data type:
- Data types that are build form other data types.
By value/ By reference.
Bulld-in data types VS user-defined data types
build-in data types
(1) The programming language defines the range of possible values that can be assigned to a variable when its type has been chosen
(2)
User defined data types
1. What does it mean by the user:
The programmer who use the certain programming language to write the programme
2. A user-defined data type is defined by the user:
The possible values; The operations that can be performed
3. Not all programming languages support user-defined data type
4. Once a user-defined data type is define, variables of this type can be decalred and uses.
Why is user-defined data types are necessary
- No suitable data type is provide by the language used
- The progeammer needs to specify a new data type to meet the requirement of the application/program.
Conposite data types VS non-composite data types
A composit data type or compound data type:
any data type that is constructured using primative data types and other composite types.
Non-composite data type:
Defined without other data types.
Example:
- An integer is a non-composite
- A record is a composited data type
- A class is a composited data type
Non-composited data type VS enumerated data type:
A non-composite data type is one which has a definition which does
Enumerated data type
Two examples of enumerated data type definitions:
- pseudocode shows two examples of enumerated data type definitions:
TYPE Directions = (North, East, South, West)
TYPE TDays = (Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday)
Note: the format of the definitions on our text book is not correct
Variables can be declared and assigned values, for example:
- DECLARE Direction1 : TDirections
- DECLARE StartDay: TDays
- Direction1 & North
- StartDay + Wednesday
- NOTE: The values of the enumerated type look like string values but they are not. The values must NOT be enclosed in quotes.
Composite user defined data types
A composite user-defined data type has a definition with reference to at least one other type.
Very important examples of composite user-defined data type.
- The record data type (introduced in Chapter 13). Although there could be built-in record data types the expectation is for a record data type to be user-defined.
- This allows the programmer to create record data types with components that precisely match the data requirements of the particular program.
- Note that Python is a language that does not support the use of a record data type.
The class.
- A class is a data type which is used for an object in object-oriented programming.
- For a given object-oriented programming language there are likely to be a number of built in classes.
- However, if a programmer intends to utilize the benefits of the object-oriented approach, then the programmer will have to create a number of user-defined classes.
We’ll talk more details about class later in chapter 27
Pointer data type
A pointer variable is one for which the value is a reference to a memory location.
Example:
- TYPE TintegerPointer =^ Integer
- The ^ caret is used
- A variable of TIntegerPointer stores a memory address of an Integer variable.
Pointer data type
-Not all programming languages offer support for the use of a pointer data type. Those languages that do so will have their own version of the symbolism illustrated above with ^and@.
- Because arithmetic can be performed on pointer variables, it is possible to use pointer variables to construct dynamically varying data structures. For some programming languages it is necessary to declare an array with a large upper bound to ensure that the array is unlikely to be fully populated with values. If the language supports the use of a pointer variable, the size of an array can expand while a program is running. The details of how this can be done are beyond the scope of this discussion.
Set data type
- A set data type allows a program to create sets and to apply the mathematical operations defined in set theory. A set is a mathematical concept with important properties.
Features:
- It contains a collection of data values.
- There is no organization of the data values within the set. -> how the data values are organized is unveiled.
- Duplicare values are not allowed.
- Operations that can be performed on a ser include:
- – checking if a value exists in a set
- – adding a new data value
- – removing an existing data value
- – adding one set to another set.
- – get the intersection of 2 sets
- – get the union of 2 sets
- Python offers the support of set data type.
Conclusion
Different kinds of data types
- Primitive Data types: 硬件本体支持的
- non-primitive Data types: 建立在其他
- Bulld-in data types:编程语言原生支持
- User-Defined Data Types:用户在编程中自定义类型
- Conposite data types(复合数据类型):以不同方式组合基本数据类型得到的新数据类型
- enumerated data type(枚举数据类型):基本数据类型之一,枚举型数据可以用来表示一组常量,比如一周的星期天数、一个员工的职位等。
- class(类):面向对象编程中的一种数据类型
- Pointer data type:指针类型:用于表示内存地址