SlideShare a Scribd company logo
Data Types
             (Chapter 6)
Name:                            ID:
AHMAD ARAFAT BIN MOHD ALI       1091105499
NUR FAIRUZ BINTI MUHAMED ASRI   1091104133
SYUHAIDA AHMAD ARIFFIN          1101108762
SURAYA NURAIN BINTI KALID       1101109398
• What is data type?

      int x = 5;
Chapter 6 data types
Boolean
Character
- Charter types are stored as numeric codings
They are used to represent switched and flags in programs.
- (ASCII / Unicode).
                                                Complex
                                                                        Single Precision
                        Numeric               Floating Point
                         Types                                          Double Precision

                                                 Decimal


Floating Point:                                     Integer
1) Single Precision:
 Primitive Data Types       Boolean                                    Integer
               Decimal:
Complex Type:                                                           Bytes
               Example: (in C#) decimal myMoney = 300.5m;                               Java
Example (in Phyton):
2) Double Precision:                                                     Long
(7+3j)                                             ASCII
               Advantage: Greater precision and a smaller range compared to Floating Point
                             Character
                                                                        Short
Programming Languages that support Complex Type: Unicode
Fortran, Phyton
Chapter 6 data types
Limited Dynamic String Strings
 Dynamic Length Length
 Static Length String:          Design Issues               1) Static or dynamic?
--The length canvarious lengthset whenmaximum. is created Special Type of character
  String strings can store any number of chars between2)
 -Allowsvariablesbe static and with no the string            zero and the maximum
 -Requires ‘0’ is used to indicate the end of string’s characters (used primitive types?
                                                                array or by C)
-Delimiter the overhead of dynamic storage allocation and deallocation but provides
 flexibility
 Example Fotran-90: Character (LEN=15) name;
                                          Comparison
                                                               Assignment
Example: JavaScript and Perl

                               Operations                     Concatenation
                                on String
    Character String                                            Substring
         Types

                                              Copy               Length


Example:
Example:
Assume s=“good”, t=“bye”
Assume s=“good”, t=“bye”
Assume s=“good”, t=“bye”
                                                                Static Length String
strcpy(x,s); = //return false String length
if (s.equals(t)) “goo”;
strlen(s)=3;,‘hello’-
Strcat(s,t)=goodbye
 Char x *+ =
Subs(s1,0,2)                                                       (Fixed Length)

                                 Options                         Limited Dyna mic
x=good                                                             Length String

                                                                  Dynamic Length
                                                                      String
Chapter 6 data types
User-Defined
                                    Ordinal Types



      Enumeration                                                Subrange
         Types                                                     Types



2) Enumeration Types - provides a of defining and grouping collections of named
1) Subrange Types - provides a way way of defining and grouping collections of named
constant.
    constant.

Example In Ada:
             C#:
type Days is (Mon, Tue, Wed Thu, Fri, Sat, Sun);
enum days {Mon, Tue,Wed, Thu, Fri, Sat, Sun};
subtype Weekdays is Days range Mon..Fri;
subtype Index
Advantage: is Integer range 1..100;
a) Readibility – Named values are easily recognized
Advantages:
b) Realibility – No arithmetic operations are legal on enumeration type
a) Enhance Readibility – variable of subtype can store only certain range.
b) Realibility (use integer values to represent enumeration to assign
Example in C– because a compiler can detect error if we try type): value to a specified
subrange value.
int red = 0, blue = 1
Categories Of Array
Categories of      Range of           Storage Bindings   Advantages      Example
Array              subscripts

1. Static Array    Statically bound   Static (done       Execution       Fortran-77, C, C++
                                      before runtime)    efficiency
2. Fixed Stack     Statically bound   Done during        Storage space   C , C++
Dynamic Array                         execution          can be reused
3. Stack Dynamic   Dynamic            Dynamic            Flexibility     Ada
Array
4. Fixed Heap      Fixed after        Fixed after        Flexibility     Fortran 95, C,
Dynamic Array      storage is         storage is                         C++, Java, C#
                   allocated          allocated
                   -done when         -allocated from
                   user program       the heap
                   requests during
                   execution
5. Heap Dynamic    Dynamic            Dynamic            Flexibility     C#, Java,
Array                                                                    JavaScript,
                                                                         Python, Ruby
Array Initialization

Programming Language             How it is initialize?

1. Fortran 95                    Integer, Dimension (3) :: List = (/0, 5, 5 /)

2. C and C++                     int list [ ] = {4, 5, 7, 83};
                                 int list [3] = {3, 5, 7};
                                 char name * + = “freddie”;

3. Java                          String[ + names = *“Bob”, “Jake”+;

          Array Operation

Programming Language                   Example Operation
Fortran 95                             ( + ) operator
                                       Give result sum of element
                                       pairs of the two arrays.
Rectangular Array       Jagged Array




Language supported:   Language supported:
 Fortran              Java
 Ada                  C
 C#                   C++
                       C#
[0] [1]          [2]
[0] 2 6              10
[1] 3 5              8
                                 Implementation Of Array Types
[2] 12 1             7
          Row Major Order
                                                           Column Major Order
          Int num[3][3];
                                                           Int num[3][3];
            [0]            [1]     [2]                     [1]      [2]     [3]
    [0]         2          6       10               [1]     2       6       10

    [1]         3          5       8                [2]     3       5       8

    [2]         12         1       7                [3]     12      1       7


     2      6        10 3 5 8 12 1 7
                                                          2 3    12 6 5 1 10 8 7
          Language : C, Pascal, Java                      Language : Fortran
Record type

What is record?     Record is an aggregate of data elements
                     in which the individual elements are identified
                    by names and accessed through offset
                    from the beginning of the structure.
Differences between Record and Array

Record                         Array
Fields are referenced by using Fields are referenced by
their names                     indices
Use when the collection of      Use when all the data
data objects is heterogeneous elements have same type and
and fields are not processed in are processed in same way.
same way.
Fields of records are not      Processing of array elements is
processed in any particular    usually done by in a sequential
sequential order.              order
Operations on Records
Programming      Operation
Languages
Ada              1. Allow comparison
                  = (equality)
                  /= (inequality)
                 2. Initialize with aggregate literals.
COBOL            1. Provide
                 MOVE CORRESPONDING
                 Example:
                 MOVE CORRESPONDING INPUT_RECORD TO OUTPUT_RECORD.
                 01 INPUT_RECORD.
                    02 NAME.
                        05 LAST             PICTURE IS X (20).
                        05 MIDDLE           PICTURE IS X (15).
                        05 FIRST            PICTURE IS X (20) .
                    02 EMPLOYEE_NUMBER      PICTURE IS 9 (10).
                    02 HOURS_WORKED          PICTURE IS 99.

                 01 OUTPUT_RECORD.
                     02 NAME.
                        05 LAST               PICTURE IS X (20).
                        05 MIDDLE             PICTURE IS X (15).
                        05 FIRST              PICTURE IS X (20) .
                    02 EMPLOYEE_NUMBER        PICTURE IS 9 (10).
                    02 GROSS_PAY              PICTURE IS 999V999.
                   02 NET_PAY                 PICTURE IS 999V999.
Language   Name Type Equivalent   Structure Type Equivalent
Ada        Works well             More rigid
C          Works well             Works well
C++        Works well             Works well
Fortran    Cannot use             Works well
COBOL      Cannot use             Works well
6.8 Union Type
Introduction

•    A union is a type whose variables are allowed to store different type
     values at different times during execution.
•    Example:
              union foo {
                                                           union foo x;
                 int a;
                                                          x.a = 3; // OK
                 char b;                                     x.b = 'c';
                 } foo;                        // NO! this affects the value of x.a!
    //can’t use both a and b at the
              same time                      Prints out 99,99

           struct bar {                      struct bar y;
              int a;                         y.a = 3;      // OK
              char b;                        y.b = 'c';     // OK
              } bar;
//can use a and b simultaneously             Prints out 387427, 99
6.8.2 Discriminated versus free unions
                                            Type of Unions




           Discriminated
                                                                 Free Unions
              Unions


   It has a type checking support for unions                 Fortran, C, and C++ provide
     require that each union include a type
                                                               union constructs in which
                    indicator.
  The first language to provide discriminated
                                                              there is no language support
union was ALGOL 68 which is later supported                  for type checking therefore it
                    by ADA.                                     is unsafe to use in these
                                                                        languages.



   Unconstrained                  Constrained
  Variant Variable               Variant Variable
6.8.3 Ada union type
                               Ada union type


   Constrained variant                                 Unconstrained variant
        variable                                             records


                                       Allow the values of their variants to change types
Allows the user to specify             during execution. The type can only be changed
    variables of a variant             only by assigning the entire record, including the
 record type that will store                             discriminant.
  only one of the possible
                                       This disallows inconsisient records because if the
 type values in the variant.               newly assigned record is a constant data
  In this way, the user can             aggregate, the value of the tag and the type of
  tell the system when the             variant can be statically checked for consistency.
    type checking can be
  static, because we know              If the assigned value is a variable, its consistency
that type checking must be             was guaranteed when it was assigned, so the new
           dynamic.                     value of the variable now being assigned is sure
                                                        to be consistent.
How does union
                                   implemented in              At compile time,
                                    programming                the complete
                                     languages?                description of each
                                                               variant must be
Use the same address                                           stored by creating a
for every variant.                                             case table.




                                            In Ada language, the
          Sufficient storage for            exact amount of
          the largest variant is            storage can be used
          allocated.                        because there is no
                                            variation.
The descriptor for this type could have the form shown in the
figure below:
6.9 Pointer and reference type
Introduction
• 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)
6.9.2 Pointer operations
• Language that provide pointer has two fundamental
  operations: assignment and dereferencing
• Assignment is used to set a pointer variable’s value to
  some useful address
• Dereferencing yields the value stored at the location
  represented by the pointer’s value
   – Dereferencing can be explicit or implicit
   – C++ uses an explicit operation via * (asterisk)
     j = *ptr
     sets j to the value located at ptr
Problems with pointer


    Dangling                                  lost heap-
    pointers                                  dynamic
    (dangerous)                               variable



A pointer points                      An allocated heap-dynamic
   to a heap-                         variable that is no longer
dynamic variable                      accessible to the user
                                      program (often called
 that has been
                                      garbage)
  deallocated.


                                1.   Pointer p1 is set to point to a newly
                                     created heap-dynamic variable

                                2.   Pointer p1 is later set to point to another
                                     newly created heap-dynamic variable

                                3.   The first heap –dynamic variable is now
                                     inaccessible (memory leakage).
Pointers



           Ada                                                        C++



•   It is called access
    types                     •   Extremely flexible but must be used with care
•   Some dangling             •   Pointers can point at any variable regardless of
    pointers are                  when it was allocated
    disallowed because        •   Used for dynamic storage management and
    dynamic objects can           addressing
    be automatically de-      •   Pointer arithmetic is possible
    allocated at the end of   •   Explicit dereferencing and address-of operators
    pointer's type scope      •   Domain type need not be fixed (void *)
•   The lost heap-            •   void * can point to any type and can be type
    dynamic variable              checked (cannot be de-referenced)
    problem is not
    eliminated by Ada.
6.9.6 Reference types

• C++ includes a special kind of pointer type called a
  reference type that is used primarily for formal
  parameters
   – Advantages of both pass-by-reference and pass-by-value
• Java extends C++’s reference variables and allows
  them to replace pointers entirely
   – References refer to call instances
• C# includes both the references of Java and the
  pointers of C++
6.9.7 Evaluation of pointers

• Dangling pointers and dangling objects are the
  same problems as is heap management
• Pointers are like goto's--they widen the range
  of cells that can be accessed by a variable
• Pointers or references are necessary for
  dynamic data structures--so we can't design a
  language without them.
Implementation of pointers and reference type



Representation of                Solutions to dangling
  pointers and                                                             Heap management
                                        pointers
  referencces

                      Tombstone: extra heap cell that is a pointer     A very complex run-time
•   Large             to the heap-dynamic variable                     process
    computers
    use single
                             The actual pointer variable points only   Single-size cells vs. variable-
                             at tombstones                             size cells
    values
                             When heap-dynamic variable de-
•   Intel                                                              Two approaches to reclaim
                             allocated, tombstone remains but set
    microproce                                                         garbage
                             to nil
    ssors use                                                                Reference counters
    segment                  Costly in time and space
                                                                             (eager approach):
    and offset         Locks-and-keys: Pointer values are
                      represented as (key, address) pairs                    reclamation is gradual
                             Heap-dynamic variables are                      Garbage collection (lazy
                             represented as variable plus cell for           approach): reclamation
                             integer lock value                              occurs when the list of
                             When heap-dynamic variable                      variable space becomes
                             allocated, lock value is created and            empty
                             placed in lock cell and key cell of
                             pointer
Reference counter
• Reference counters: maintain a counter in every cell
  that store the number of pointers currently pointing
  at the cell
   – Disadvantages: space required, execution time required,
     complications for cells connected circularly
   – Advantage: it is intrinsically incremental its actions are
     interleaved with those of the applicasion so it never causes
     significant delays in the execution of the application.
Garbage collection
• The run-time system allocates storage cells as requested
  and disconnects pointers from cells as necessary; garbage
  collection then begins
   – Every heap cell has an extra bit used by collection algorithm
   – All cells initially set to garbage
   – All pointers traced into heap, and reachable cells marked as not
     garbage
   – All garbage cells returned to list of available cells
   – Disadvantages: when you need it most, it works worst (takes
     most time when program needs most of cells in heap)
Marking algorithm
Variable sized cells
• All the difficulties of single-size cells plus more
• Required by most programming languages
• If garbage collection is used, additional problems
  occur
   – The initial setting of the indicators of all cells in the heap is
     difficult
   – The marking process in nontrivial
   – Maintaining the list of available space is another source of
     overhead
6.10 Type Checking
Type Checking




 Activity of ensuring that the                             Type Error
operands of an operator are of
       compatible types


                                                 Application of an operator to an
                                                  operand of an inappropriate
                                                               type
Types of Type
                                    Checking



Static type checking                          Dynamic type checking

•Perform type checking                        • Perform type checking during
 before runtime                                 runtime
•For static type binding                      • For dynamic type binding
 languages                                      languages

Exp: Earlier assembler, FORTRAN               • Exp: JavaScript, PHP


Advantages:                                       Advantage:
-earlier detection of                             - easier to handle situations that
programming mistakes                                require self-modifying code
- increased runtime efficiency

Disadvantage:                                     Disadvantage:
-Longer compilation time                          -longer runtime
6.11 Strong Typing
Strong
                                            Type


                          Programming language is strongly
                            typed if type errors are always
                                       detected.


Advantage:
                                                       Disadvantage:
-Ability to detect all misuses of
                                                       - Weakened by coercion
variables that result in type errors
-Also allows the detection at run time


  Language                     Range                           Comments
  Fortran 95                   Not strongly typed              Because the use of Equivalence
                                                               between variables of different types
  Ada                          Nearly strong typed             Allows the breach of type-checking
                                                               rules using function
                                                               Unchecked_Conversion
  C, C++                       Not strongly typed              Both include union types, which are
                                                               not typed checked
  Java, C#                     Nearly strongly typed           Types can be explicitly cast, which
                                                               could result in a type error
6.12 Type Equivalence
Type                                        Type
              Compatibility                               Equivalence




                                            An operand of one type in an expression
Types of operand that are acceptable for
                                             is substituted for one of the other type
each of the operators and thereby specify
                                            without coercion – compatibility without
 the possible type errors of the language
                                                             coercion
Types of Type
                                             Equivalence
 Name type equivalence                                      Structure type equivalence

• Two name have equivalence                                 • Equivalence if their types have
  type if they are defined under                              identical structure.
  the same declaration or
  declaration that use the same
  type name.
                                                            Advantage:
                                                            - more flexible
Advantages:
- Easy to implement
- More restrictive
                                                     Disadvantage:
Disadvantage:                                        - More difficult to implement
- Subrange of the integers would not be              - Entire structure of the two types
  equivalent to an integer type variable.              must be compared



   type Indextype is 1..100;
                                                            type Celsius = Float;
   count : Integer;
                                                                 Fahrenheit = Float;
   index : Indextype;
Derived
                              Type



•   New type that is based on previously defined type
•   Not equivalent although it may have identical structure
•   Inherit all the properties of their parent types
•   Can include range constraints on the parent type, while still
    inheriting all the parent’s properties




              type Celsius is new Float;
              type Fahrenheit is new Float;
Subtype



      Type equivalent to its
          parent type




subtype Small_type is Integer
range 0.99;
The type Small_type is
equivalent to the type Integer
6.13 Theory & Data Types
Theory




              •Type lambda calculus
              •Combinators
              •The Methatheory of Bounded Quantification
              •Existential Types
              •Higher-Order Polymorphism




 Practical
                                            Abstract
• Concerned with data
                                           • Focuses on type lambda
 types in commercial
                                             calculus
 programming languages
Data Type




                                                         Type System
Set of values and a collection of
  operations on those values.



                                                Set of types and the values that
                                                 govern their use in programs
Model Of Type
                                 System


 Formal model                                 Alternative model

Set of types and a                             • Uses a type map and a
collection of functions that                     collection of functions
define the type rules of the
language, which are used
to determine the type of
any expression


                                   Typed
                                 Language

 Static typed language                         Dynamic typed language

• Type map need only be                        • Type map must be
  maintained during                              maintained during
  compilation time                               execution
Chapter 6 data types

More Related Content

PPTX
1.2. introduction to automata theory
PPT
Packages,interfaces and exceptions
PPTX
Python SQite3 database Tutorial | SQlite Database
PPT
Java RMI
PDF
Distributed Operating System_4
PPTX
Inheritance in C++
PDF
Introduction to MPI
PPTX
Concurrency control
1.2. introduction to automata theory
Packages,interfaces and exceptions
Python SQite3 database Tutorial | SQlite Database
Java RMI
Distributed Operating System_4
Inheritance in C++
Introduction to MPI
Concurrency control

What's hot (20)

PPTX
Process synchronization
PPTX
[OOP - Lec 01] Introduction to OOP
PPTX
Operating system 22 threading issues
PPTX
Exceptions in Java
PDF
Java I/o streams
PPT
File handling in c
PPTX
Priority queue in DSA
PDF
Part 01 Linux Kernel Compilation (Ubuntu)
PPT
Hash tables
PPTX
Case statements
PPTX
11. java methods
PPTX
Regular expressions
PPT
Basic of Multithreading in JAva
PDF
Linux device driver
PPT
Lecture 5
PPT
SQLITE Android
PPTX
Context free grammar
PPS
Java Exception handling
PPTX
Backus Naur and Chomsky Normal Forms
PPTX
Python Scipy Numpy
Process synchronization
[OOP - Lec 01] Introduction to OOP
Operating system 22 threading issues
Exceptions in Java
Java I/o streams
File handling in c
Priority queue in DSA
Part 01 Linux Kernel Compilation (Ubuntu)
Hash tables
Case statements
11. java methods
Regular expressions
Basic of Multithreading in JAva
Linux device driver
Lecture 5
SQLITE Android
Context free grammar
Java Exception handling
Backus Naur and Chomsky Normal Forms
Python Scipy Numpy
Ad

Viewers also liked (20)

PPTX
Nervous system
PDF
bureau rowin petersma 2015
PPTX
Google refine tutotial
PPT
Understanding the sociocultural context through partnership with communities
PDF
PPT
Презентация Wikimart
PPT
YouTube進階應用2
PPTX
Bose corporation
PPTX
Google refine from a business perspective
PPTX
Servicios de-streaming
PPTX
The Reproductive System
PPT
Presentatie Ruilklassen Weerijs
PDF
Ruth White Cv11.11.11
PPTX
PPTX
Google refine from a business perspective
PDF
Angles complementaris
PPTX
Rogers royals basketball
PPTX
LessonPlanning2
PDF
Sedação e cp
DOC
Nagiosql和centreon比较
Nervous system
bureau rowin petersma 2015
Google refine tutotial
Understanding the sociocultural context through partnership with communities
Презентация Wikimart
YouTube進階應用2
Bose corporation
Google refine from a business perspective
Servicios de-streaming
The Reproductive System
Presentatie Ruilklassen Weerijs
Ruth White Cv11.11.11
Google refine from a business perspective
Angles complementaris
Rogers royals basketball
LessonPlanning2
Sedação e cp
Nagiosql和centreon比较
Ad

Similar to Chapter 6 data types (20)

PDF
Plc (1)
PPTX
Plc (1)
PDF
Chapter 2 basic element of programming
PDF
Jvm internals
PPTX
Oop c++class(final).ppt
PDF
Cpprm
PPTX
C_Progragramming_language_Tutorial_ppt_f.pptx
PPTX
java Basic Programming Needs
PPTX
OOP Introduction Part 2 In Java Language.pptx
KEY
Xbase - Implementing Domain-Specific Languages for Java
DOCX
Fundamental classes in java
PPTX
C programming language tutorial
PPTX
PPTX
PPT
object oriented programming in java lecture
PDF
Datatype
PPTX
Introduction to c
DOCX
PPTX
Java fundamentals
PPTX
Console I/o & basics of array and strings.pptx
Plc (1)
Plc (1)
Chapter 2 basic element of programming
Jvm internals
Oop c++class(final).ppt
Cpprm
C_Progragramming_language_Tutorial_ppt_f.pptx
java Basic Programming Needs
OOP Introduction Part 2 In Java Language.pptx
Xbase - Implementing Domain-Specific Languages for Java
Fundamental classes in java
C programming language tutorial
object oriented programming in java lecture
Datatype
Introduction to c
Java fundamentals
Console I/o & basics of array and strings.pptx

Recently uploaded (20)

PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
A comparative analysis of optical character recognition models for extracting...
PPTX
Spectroscopy.pptx food analysis technology
PDF
Assigned Numbers - 2025 - Bluetooth® Document
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PPTX
Big Data Technologies - Introduction.pptx
PPTX
Programs and apps: productivity, graphics, security and other tools
PDF
NewMind AI Weekly Chronicles - August'25-Week II
PPTX
sap open course for s4hana steps from ECC to s4
PPTX
Cloud computing and distributed systems.
PDF
Approach and Philosophy of On baking technology
PPTX
A Presentation on Artificial Intelligence
Network Security Unit 5.pdf for BCA BBA.
Unlocking AI with Model Context Protocol (MCP)
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
20250228 LYD VKU AI Blended-Learning.pptx
A comparative analysis of optical character recognition models for extracting...
Spectroscopy.pptx food analysis technology
Assigned Numbers - 2025 - Bluetooth® Document
“AI and Expert System Decision Support & Business Intelligence Systems”
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
Diabetes mellitus diagnosis method based random forest with bat algorithm
Reach Out and Touch Someone: Haptics and Empathic Computing
Big Data Technologies - Introduction.pptx
Programs and apps: productivity, graphics, security and other tools
NewMind AI Weekly Chronicles - August'25-Week II
sap open course for s4hana steps from ECC to s4
Cloud computing and distributed systems.
Approach and Philosophy of On baking technology
A Presentation on Artificial Intelligence

Chapter 6 data types

  • 1. Data Types (Chapter 6) Name: ID: AHMAD ARAFAT BIN MOHD ALI 1091105499 NUR FAIRUZ BINTI MUHAMED ASRI 1091104133 SYUHAIDA AHMAD ARIFFIN 1101108762 SURAYA NURAIN BINTI KALID 1101109398
  • 2. • What is data type? int x = 5;
  • 4. Boolean Character - Charter types are stored as numeric codings They are used to represent switched and flags in programs. - (ASCII / Unicode). Complex Single Precision Numeric Floating Point Types Double Precision Decimal Floating Point: Integer 1) Single Precision: Primitive Data Types Boolean Integer Decimal: Complex Type: Bytes Example: (in C#) decimal myMoney = 300.5m; Java Example (in Phyton): 2) Double Precision: Long (7+3j) ASCII Advantage: Greater precision and a smaller range compared to Floating Point Character Short Programming Languages that support Complex Type: Unicode Fortran, Phyton
  • 6. Limited Dynamic String Strings Dynamic Length Length Static Length String: Design Issues 1) Static or dynamic? --The length canvarious lengthset whenmaximum. is created Special Type of character String strings can store any number of chars between2) -Allowsvariablesbe static and with no the string zero and the maximum -Requires ‘0’ is used to indicate the end of string’s characters (used primitive types? array or by C) -Delimiter the overhead of dynamic storage allocation and deallocation but provides flexibility Example Fotran-90: Character (LEN=15) name; Comparison Assignment Example: JavaScript and Perl Operations Concatenation on String Character String Substring Types Copy Length Example: Example: Assume s=“good”, t=“bye” Assume s=“good”, t=“bye” Assume s=“good”, t=“bye” Static Length String strcpy(x,s); = //return false String length if (s.equals(t)) “goo”; strlen(s)=3;,‘hello’- Strcat(s,t)=goodbye Char x *+ = Subs(s1,0,2) (Fixed Length) Options Limited Dyna mic x=good Length String Dynamic Length String
  • 8. User-Defined Ordinal Types Enumeration Subrange Types Types 2) Enumeration Types - provides a of defining and grouping collections of named 1) Subrange Types - provides a way way of defining and grouping collections of named constant. constant. Example In Ada: C#: type Days is (Mon, Tue, Wed Thu, Fri, Sat, Sun); enum days {Mon, Tue,Wed, Thu, Fri, Sat, Sun}; subtype Weekdays is Days range Mon..Fri; subtype Index Advantage: is Integer range 1..100; a) Readibility – Named values are easily recognized Advantages: b) Realibility – No arithmetic operations are legal on enumeration type a) Enhance Readibility – variable of subtype can store only certain range. b) Realibility (use integer values to represent enumeration to assign Example in C– because a compiler can detect error if we try type): value to a specified subrange value. int red = 0, blue = 1
  • 9. Categories Of Array Categories of Range of Storage Bindings Advantages Example Array subscripts 1. Static Array Statically bound Static (done Execution Fortran-77, C, C++ before runtime) efficiency 2. Fixed Stack Statically bound Done during Storage space C , C++ Dynamic Array execution can be reused 3. Stack Dynamic Dynamic Dynamic Flexibility Ada Array 4. Fixed Heap Fixed after Fixed after Flexibility Fortran 95, C, Dynamic Array storage is storage is C++, Java, C# allocated allocated -done when -allocated from user program the heap requests during execution 5. Heap Dynamic Dynamic Dynamic Flexibility C#, Java, Array JavaScript, Python, Ruby
  • 10. Array Initialization Programming Language How it is initialize? 1. Fortran 95 Integer, Dimension (3) :: List = (/0, 5, 5 /) 2. C and C++ int list [ ] = {4, 5, 7, 83}; int list [3] = {3, 5, 7}; char name * + = “freddie”; 3. Java String[ + names = *“Bob”, “Jake”+; Array Operation Programming Language Example Operation Fortran 95 ( + ) operator Give result sum of element pairs of the two arrays.
  • 11. Rectangular Array Jagged Array Language supported: Language supported:  Fortran  Java  Ada  C  C#  C++  C#
  • 12. [0] [1] [2] [0] 2 6 10 [1] 3 5 8 Implementation Of Array Types [2] 12 1 7 Row Major Order Column Major Order Int num[3][3]; Int num[3][3]; [0] [1] [2] [1] [2] [3] [0] 2 6 10 [1] 2 6 10 [1] 3 5 8 [2] 3 5 8 [2] 12 1 7 [3] 12 1 7 2 6 10 3 5 8 12 1 7 2 3 12 6 5 1 10 8 7 Language : C, Pascal, Java Language : Fortran
  • 13. Record type What is record? Record is an aggregate of data elements in which the individual elements are identified by names and accessed through offset from the beginning of the structure.
  • 14. Differences between Record and Array Record Array Fields are referenced by using Fields are referenced by their names indices Use when the collection of Use when all the data data objects is heterogeneous elements have same type and and fields are not processed in are processed in same way. same way. Fields of records are not Processing of array elements is processed in any particular usually done by in a sequential sequential order. order
  • 15. Operations on Records Programming Operation Languages Ada 1. Allow comparison  = (equality)  /= (inequality) 2. Initialize with aggregate literals. COBOL 1. Provide MOVE CORRESPONDING Example: MOVE CORRESPONDING INPUT_RECORD TO OUTPUT_RECORD. 01 INPUT_RECORD. 02 NAME. 05 LAST PICTURE IS X (20). 05 MIDDLE PICTURE IS X (15). 05 FIRST PICTURE IS X (20) . 02 EMPLOYEE_NUMBER PICTURE IS 9 (10). 02 HOURS_WORKED PICTURE IS 99. 01 OUTPUT_RECORD. 02 NAME. 05 LAST PICTURE IS X (20). 05 MIDDLE PICTURE IS X (15). 05 FIRST PICTURE IS X (20) . 02 EMPLOYEE_NUMBER PICTURE IS 9 (10). 02 GROSS_PAY PICTURE IS 999V999. 02 NET_PAY PICTURE IS 999V999.
  • 16. Language Name Type Equivalent Structure Type Equivalent Ada Works well More rigid C Works well Works well C++ Works well Works well Fortran Cannot use Works well COBOL Cannot use Works well
  • 17. 6.8 Union Type Introduction • A union is a type whose variables are allowed to store different type values at different times during execution. • Example: union foo { union foo x; int a; x.a = 3; // OK char b; x.b = 'c'; } foo; // NO! this affects the value of x.a! //can’t use both a and b at the same time Prints out 99,99 struct bar { struct bar y; int a; y.a = 3; // OK char b; y.b = 'c'; // OK } bar; //can use a and b simultaneously Prints out 387427, 99
  • 18. 6.8.2 Discriminated versus free unions Type of Unions Discriminated Free Unions Unions It has a type checking support for unions Fortran, C, and C++ provide require that each union include a type union constructs in which indicator. The first language to provide discriminated there is no language support union was ALGOL 68 which is later supported for type checking therefore it by ADA. is unsafe to use in these languages. Unconstrained Constrained Variant Variable Variant Variable
  • 19. 6.8.3 Ada union type Ada union type Constrained variant Unconstrained variant variable records Allow the values of their variants to change types Allows the user to specify during execution. The type can only be changed variables of a variant only by assigning the entire record, including the record type that will store discriminant. only one of the possible This disallows inconsisient records because if the type values in the variant. newly assigned record is a constant data In this way, the user can aggregate, the value of the tag and the type of tell the system when the variant can be statically checked for consistency. type checking can be static, because we know If the assigned value is a variable, its consistency that type checking must be was guaranteed when it was assigned, so the new dynamic. value of the variable now being assigned is sure to be consistent.
  • 20. How does union implemented in At compile time, programming the complete languages? description of each variant must be Use the same address stored by creating a for every variant. case table. In Ada language, the Sufficient storage for exact amount of the largest variant is storage can be used allocated. because there is no variation.
  • 21. The descriptor for this type could have the form shown in the figure below:
  • 22. 6.9 Pointer and reference type Introduction • 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)
  • 23. 6.9.2 Pointer operations • Language that provide pointer has two fundamental operations: assignment and dereferencing • Assignment is used to set a pointer variable’s value to some useful address • Dereferencing yields the value stored at the location represented by the pointer’s value – Dereferencing can be explicit or implicit – C++ uses an explicit operation via * (asterisk) j = *ptr sets j to the value located at ptr
  • 24. Problems with pointer Dangling lost heap- pointers dynamic (dangerous) variable A pointer points An allocated heap-dynamic to a heap- variable that is no longer dynamic variable accessible to the user program (often called that has been garbage) deallocated. 1. Pointer p1 is set to point to a newly created heap-dynamic variable 2. Pointer p1 is later set to point to another newly created heap-dynamic variable 3. The first heap –dynamic variable is now inaccessible (memory leakage).
  • 25. Pointers Ada C++ • It is called access types • Extremely flexible but must be used with care • Some dangling • Pointers can point at any variable regardless of pointers are when it was allocated disallowed because • Used for dynamic storage management and dynamic objects can addressing be automatically de- • Pointer arithmetic is possible allocated at the end of • Explicit dereferencing and address-of operators pointer's type scope • Domain type need not be fixed (void *) • The lost heap- • void * can point to any type and can be type dynamic variable checked (cannot be de-referenced) problem is not eliminated by Ada.
  • 26. 6.9.6 Reference types • C++ includes a special kind of pointer type called a reference type that is used primarily for formal parameters – Advantages of both pass-by-reference and pass-by-value • Java extends C++’s reference variables and allows them to replace pointers entirely – References refer to call instances • C# includes both the references of Java and the pointers of C++
  • 27. 6.9.7 Evaluation of pointers • Dangling pointers and dangling objects are the same problems as is heap management • Pointers are like goto's--they widen the range of cells that can be accessed by a variable • Pointers or references are necessary for dynamic data structures--so we can't design a language without them.
  • 28. Implementation of pointers and reference type Representation of Solutions to dangling pointers and Heap management pointers referencces Tombstone: extra heap cell that is a pointer A very complex run-time • Large to the heap-dynamic variable process computers use single The actual pointer variable points only Single-size cells vs. variable- at tombstones size cells values When heap-dynamic variable de- • Intel Two approaches to reclaim allocated, tombstone remains but set microproce garbage to nil ssors use Reference counters segment Costly in time and space (eager approach): and offset Locks-and-keys: Pointer values are represented as (key, address) pairs reclamation is gradual Heap-dynamic variables are Garbage collection (lazy represented as variable plus cell for approach): reclamation integer lock value occurs when the list of When heap-dynamic variable variable space becomes allocated, lock value is created and empty placed in lock cell and key cell of pointer
  • 29. Reference counter • Reference counters: maintain a counter in every cell that store the number of pointers currently pointing at the cell – Disadvantages: space required, execution time required, complications for cells connected circularly – Advantage: it is intrinsically incremental its actions are interleaved with those of the applicasion so it never causes significant delays in the execution of the application.
  • 30. Garbage collection • The run-time system allocates storage cells as requested and disconnects pointers from cells as necessary; garbage collection then begins – Every heap cell has an extra bit used by collection algorithm – All cells initially set to garbage – All pointers traced into heap, and reachable cells marked as not garbage – All garbage cells returned to list of available cells – Disadvantages: when you need it most, it works worst (takes most time when program needs most of cells in heap)
  • 32. Variable sized cells • All the difficulties of single-size cells plus more • Required by most programming languages • If garbage collection is used, additional problems occur – The initial setting of the indicators of all cells in the heap is difficult – The marking process in nontrivial – Maintaining the list of available space is another source of overhead
  • 34. Type Checking Activity of ensuring that the Type Error operands of an operator are of compatible types Application of an operator to an operand of an inappropriate type
  • 35. Types of Type Checking Static type checking  Dynamic type checking •Perform type checking • Perform type checking during before runtime runtime •For static type binding • For dynamic type binding languages languages Exp: Earlier assembler, FORTRAN • Exp: JavaScript, PHP Advantages: Advantage: -earlier detection of - easier to handle situations that programming mistakes require self-modifying code - increased runtime efficiency Disadvantage: Disadvantage: -Longer compilation time -longer runtime
  • 37. Strong Type Programming language is strongly typed if type errors are always detected. Advantage: Disadvantage: -Ability to detect all misuses of - Weakened by coercion variables that result in type errors -Also allows the detection at run time Language Range Comments Fortran 95 Not strongly typed Because the use of Equivalence between variables of different types Ada Nearly strong typed Allows the breach of type-checking rules using function Unchecked_Conversion C, C++ Not strongly typed Both include union types, which are not typed checked Java, C# Nearly strongly typed Types can be explicitly cast, which could result in a type error
  • 39. Type Type Compatibility Equivalence An operand of one type in an expression Types of operand that are acceptable for is substituted for one of the other type each of the operators and thereby specify without coercion – compatibility without the possible type errors of the language coercion
  • 40. Types of Type Equivalence  Name type equivalence  Structure type equivalence • Two name have equivalence • Equivalence if their types have type if they are defined under identical structure. the same declaration or declaration that use the same type name. Advantage: - more flexible Advantages: - Easy to implement - More restrictive Disadvantage: Disadvantage: - More difficult to implement - Subrange of the integers would not be - Entire structure of the two types equivalent to an integer type variable. must be compared type Indextype is 1..100; type Celsius = Float; count : Integer; Fahrenheit = Float; index : Indextype;
  • 41. Derived Type • New type that is based on previously defined type • Not equivalent although it may have identical structure • Inherit all the properties of their parent types • Can include range constraints on the parent type, while still inheriting all the parent’s properties type Celsius is new Float; type Fahrenheit is new Float;
  • 42. Subtype Type equivalent to its parent type subtype Small_type is Integer range 0.99; The type Small_type is equivalent to the type Integer
  • 43. 6.13 Theory & Data Types
  • 44. Theory •Type lambda calculus •Combinators •The Methatheory of Bounded Quantification •Existential Types •Higher-Order Polymorphism  Practical  Abstract • Concerned with data • Focuses on type lambda types in commercial calculus programming languages
  • 45. Data Type Type System Set of values and a collection of operations on those values. Set of types and the values that govern their use in programs
  • 46. Model Of Type System  Formal model Alternative model Set of types and a • Uses a type map and a collection of functions that collection of functions define the type rules of the language, which are used to determine the type of any expression Typed Language  Static typed language  Dynamic typed language • Type map need only be • Type map must be maintained during maintained during compilation time execution