Thursday 19 March 2015

6.0 Data Types

6.1 Introduction 
  • A Data type defines a collection of data values and a set of predefined operations on those values.
  • An important factor of determining the ease with which they can perform this task is how well the data types are available in the language being used match the objects in the real world.
  • The two most common (non-scalar) data types are arrays and records. although the popularity of associate arrays has increased significantly.

  • It is convenient, both logically and concretely, to think of variables as descriptors.
  • A descriptor is the collection of attributes of a variable.
  • If the attributes are all static, descriptors are required only at compile time.
  • For dynamic attributes however they are used at run-time
  • In all cases, descriptors are used for type checking and to build the code for the allocation and de-allocation operations
 6.2 Primitive Data Types

These are data types that are not defined in terms of other types.

6.2.1 Numeric Types

6.2.1.1 Integer
The most common primitive numeric data type is integer.

Many programming languages support different sizes of integers: byte, short, int and long.
Some languages like C++ and C#, include unsigned integer types.
Unsigned types are usually used for binary data.

A signed integer value is represented in a computer by a string of bits with one of the bits (typically the left most)  representing the sign.

unsigned int  00000001 = 1
                      11111111 =  255

Signed int     00000000 =  0
                      00000001 =  1
                      01111111  =  127
                      10000000 = -128
                      11111111  =  -1

6.2.1.2 Floating Points 

Floating point data type model real numbers, but the representations are only approximations of most real values.

Most languages include two floating point types, often called float and double.
Float - 4 bytes
The double type is provided for situations where larger fractional parts are need. double the size of a float with 8 bytes.


6.2.1.4 Decimal
Decimal data types store a fixed number of decimal digits with the decimal point at a fixed position in the value.
- Date type for business data processing
-Have an advantage of being able to precisely store decimal values, at least those within a restricted range, which cannot be done with floating points.
- its takes at least four bit to code a decimal digit.

6.2.2 Boolean Types

Boolean types are perhaps the simplest of all types.
- Their range of values has two elements - true and false
- Any non-zero value is true
- Zero is false
- A Boolean value could be represent by a single bit, but because a single bit of memory cannot be accessed efficiently on many machines, they are often stored in the smallest efficiently addressable cell of memory, typically a byte.

6.2.3 Character Types
Character data are store in computers as numeric coding.
-ASCII (American Standard code for information interchange) is the traditional character set.
- ASCII uses the value 0 to 127 to code 128 different characters.
-Because of globalization of business and the need for computers to communicate with computers around the world, the ASCII character set is becoming inadequate.

- Unicode has been developed as an alternative.
- 16 bit character set
-Includes characters from most of the worlds natural languages. ie, Thai digit, Cyrillic alphabet
- the first 128 characters are identical to ascii
- Used in C#



6.3 Character String Types
A character string type is one in which the values consist of sequences of characters.

The common string operations are assignment, catenation, sub-string reference, comparison and pattern matching.



6.4 User-definer ordinal types
- An ordinal type is one in which the range of possible values can be easily associated with the set of positive integers.

6.4.1 Enumeration Type
An enumeration type is one in which all of the possible values, which are name constants are provided, or enumerated in the definition.

- Away of defining and grouping collections of name constants. (enumeration constants)
- Sub range is a sub group in an enumeration type ie. Ratings  8,9,10 = high


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

6.5.4 Heterogeneous Arrays - are arrays in which elements need not be the same type.


6.5.6 Array Operations - assignment, catenation, comparison for equality and inequality and slices.

6.5.7 Rectangular Array
- A rectangular array is a multidimensional array in which all of the rows have the same number of elements, all of the columns have the same number of elements

Jagged Array 
- A jagged array is one in which the lengths of the rows need not be the same.
 - i.e one row with 7 elements ,another row with 6, another row with 4 

6.5.8 Slices
- A slice of an array is some substructure of that array.

6.9.6 Pointer
A pointer type is one in which the variable have a range of values that consist of memory address and a special value nil.
-The value nil is not a valid address and is used to indicate that pointer cannot currently be used to reference a memory cell


Pointer have two distinct purposes
1. Pointers provide some of the power of indirect addressing which is heavily used in assembly language programming.
2. Pointers provide a way to manage dynamic storage, A pointer can be used to access a location in the area where storage is dynamically allocated, which is usually called the heap.

Pointer Operations -  assigning and de-referencing


Dangling pointer - A dangling pointer / reference, is a pointer that contains the address of a heap dynamic variable that has been de-allocated.


6.9.7 Reference Type
A reference type variable is similar to a pointer, with one important and fundamental difference.
 A pointer refers to an address in memory, while a reference refers to an object or value in memory.

No comments:

Post a Comment