Kamis, 28 Desember 2017

Data Types I

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
Others require only a little non-hardware support for their implementation
Data types :
Integer
Floating Point
Complex
Decimal
Boolean
Character

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/deallocation 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

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 Type

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, thufri, sat, sun);
subtype Weekdays is Days range mon..fri;
subtype Index is Integer range 1..100;
Day1: Days;
Day2: Weekday;
Day2 := Day1;

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.
Indexing (or subscripting) is a mapping from indices to elements
   array_name (index_value_list) ®  an element
Index Syntax
Fortran and 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
FORTRAN, C: integer only
Ada: integer or enumeration (includes Boolean and char)
Java: integer types only
Index range checking
  - C, C++, Perl, and Fortran do not specify
        range checking
    - Java, ML, C# specify range checking
    - In Ada, the default is to require range
        checking, but it can be turned off

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++
char *names [] = {″Bob″, ″Jake″, ″Joe″];
Java initialization of String objects
String[] names = {″Bob″, ″Jake″, ″Joe″};

Pointers in C and C++


Extremely flexible but must be used with care
Pointers can point at any variable regardless of when or where it was allocated
Used for dynamic storage management and addressing
Pointer arithmetic is possible
Explicit dereferencing and address-of operators
Domain type need not be fixed (void *)
   void *  can point to any type and can be type
      checked (cannot be de-referenced)
float stuff[100];
float *p;
p = stuff;
*(p+5) is equivalent to stuff[5] and  p[5]

*(p+i) is equivalent to stuff[i] and  p[i]


Reference
Robert W. Sebesta - Concept of Programming Languages (Tenth Edition), Chapter 6
  







Tidak ada komentar:

Posting Komentar