SlideShare a Scribd company logo
Chapter 6: Data Types 
Principles of Programming Languages
Contents 
•Primitive Data Types 
•Character String Types 
•User-Defined Ordinal Types 
•Array Types 
•Associative Arrays 
•Record Types 
•Union Types 
•Pointer and Reference Types 
•Type Checking
Introduction 
•A data type defines 
–a collection of data objects 
–a set of predefined operations 
•Abstract Data Type: Interface (visible) are separated from the representation and operation (hidden) 
•Uses of type system: 
–Error detection 
–Program modularization assistant 
–Documentation
The Next Part 
•Primitive Data Types 
•Character String Types 
•User-Defined Ordinal Types 
•Array Types 
•Associative Arrays 
•Record Types 
•Union Types 
•Pointer and Reference Types 
•Type Checking
Primitive Data Types 
•Data types that are not defined in terms of other data types 
•Some primitive data types are merely reflections of the hardware 
•Others require little non-hardware support
Primitive Data Types: Integer 
•Languages may support several sizes of integer 
–Java’s signed integer sizes: byte, short, int, long 
•Some languages include unsigned integers 
•Supported directly by hardware: a string of bits 
•To represent negative numbers: twos complement
Primitive Data Types: Floating Point 
•Model real numbers, but only as approximations 
•Languages for scientific use support at least two floating-point types (e.g., float and double) 
•Precision and range 
•IEEE Floating-Point Standard 754
IEEE 754 
Sign bit 
Exponent 
Fraction 
23 bits 
8 bits 
Single-pricision Sign bit 
Exponent 
Fraction 
52 bits 
11 bits 
Double-pricision
Primitive Data Types: Decimal 
•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
Primitive Data Types: Boolean 
•Simplest of all 
•Range of values: two elements, one for “true” and one for “false” 
•Could be implemented as bits, but often as bytes 
–Advantage: readability
Primitive Data Types: 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: 
–Is it a primitive type or just a special kind of array? 
–Should the length of strings be static or dynamic?
Character String Types Operations 
•Typical operations: 
–Assignment 
–Comparison (=, >, etc.) 
–Catenation 
–Substring reference 
–Pattern matching 
•Regular Expression
•C and C++ 
–Not primitive 
–Use char arrays and a library of functions that provide operations 
•Java 
–Primitive via the String class 
–Immutable 
Character String in Some Languages
Character String Length Options 
•Static: Python, Java’s String class 
•Limited Dynamic Length: C 
–In C, a special character is used to indicate the end of a string’s characters, rather than maintaining the length 
•Dynamic (no maximum): Perl, JavaScript, standard C++ library 
•Ada supports all three string length options
Character String Type Evaluation 
•Aid to writability 
•As a primitive type with static length, they are inexpensive to provide--why not have them? 
•Dynamic length is nice, but is it worth the expense?
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-time 
descriptor for 
static strings 
Run-time descriptor 
for limited dynamic 
strings 
Descriptor
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
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}; 
days myDay = Mon, yourDay = Tue; 
•Design issues: 
–Is an enumeration constant allowed to appear in more than one type definition? 
–Are enumeration values coerced to integer? 
–Are any other types coerced to an enumeration type?
Evaluation of Enumerated Type 
•Readability 
–no need to code a color as a number 
•Reliability 
–Ada, C# and Java 5.0 
•operations (don’t allow colors to be added) 
•No enumeration variable can be assigned a value outside its defined range 
•Better support for enumeration than C++: enumeration type variables are not coerced into integer types
•Enumeration types are implemented as integers 
Implementation of User-Defined Ordinal Types
The Next Part 
•Primitive Data Types 
•Character String Types 
•User-Defined Ordinal Types 
•Array Types 
•Associative Arrays 
•Record Types 
•Union Types 
•Pointer and Reference Types 
•Type Checking
Array Types 
•Collection of homogeneous data elements 
•Each element is identified by its position relative to the first element 
•Homogeneous: data elements are of same type 
•Referenced using subscript expression
Array Indexing 
•Indexing (or subscripting) is a mapping from indices to elements 
array_name (index_value_list)  an element 
•Index Syntax 
–Fortran, Ada use parentheses 
–Most other languages use brackets
Arrays Index (Subscript) Types 
•What type are legal for subscripts? 
•Pascal, Ada: any ordinal type (integer, boolean, char, enumeration) 
•Others: subrange of integers 
•Are subscripting expressions range checked? 
•Most contemporary languages do not specify range checking but Java, ML, C# 
•Unusual case: Perl
•Static: subscript ranges are statically bound and storage allocation is static 
–Advantage: efficiency (no dynamic allocation) 
–Disadvantage: storage is bound all the time 
•Fixed stack-dynamic: subscript ranges are statically bound, but the allocation is done at declaration time 
–Advantage: space efficiency 
–Disadvantage: dynamic allocation 
Subscript Binding & Array Categories
•Stack-dynamic: subscript ranges are dynamically bound and the storage allocation is dynamic (done at run-time) 
–Advantage: flexibility (the size of an array need not be known until the array is to be used) 
•Fixed heap-dynamic: similar to fixed stack- dynamic: storage binding is dynamic but fixed after allocation (i.e., binding is done when requested and storage is allocated from heap, not stack) 
Subscript Binding & Array Categories
•Heap-dynamic: binding of subscript ranges and storage allocation is dynamic and can change any number of times 
–Advantage: flexibility (arrays can grow or shrink during program execution) 
Subscript Binding & Array Categories
Subscript Binding & Array Categories 
•C and C++ arrays that include static modifier are static 
•C and C++ arrays without static modifier are fixed stack-dynamic 
•Ada arrays can be stack-dynamic 
Get(List_Len); 
declare 
List : array (1..List_Len) of Integer; 
begin 
... 
end;
Subscript Binding & Array Categories 
•C and C++ provide fixed heap-dynamic arrays 
–malloc and free, new and delete 
•All arrays in Java are fixed heap-dynamic 
•C# includes a array class ArrayList that provides heap-dynamic 
ArrayList intList = new ArrayList(); 
intList.Add(nextOne); 
•Perl and JavaScript support heap-dynamic arrays
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"};
Arrays Operations 
•Assignment, catenation, comparison for equality, and slices 
•C-based languages do not provide any array operations 
•Java, C++ and C# use methods to manipulate
Rectangular and Jagged Arrays 
•C, C++, Java, C#: jagged arrays 
myArray[3][7] 
•Fortran, Ada, C#: rectangular array 
myArray[3,7] rectangular 
jagged
Slices 
•A slice is some substructure of an array; nothing more than a referencing mechanism 
•Slices are only useful in languages that have array operations 
•E.g. Python 
vector = [2, 4, 6, 8, 10, 12, 14, 16] 
mat = [[1, 2, 3],[4, 5, 6],[7, 8, 9]] 
vector[3:6], mat[1], mat[0][0:2]
Implementation of Arrays 
•Access function maps subscript expressions to an address in the array 
•Single-dimensioned: list of adjacent memory cells 
•Access function for single-dimensioned arrays: 
address(list[k]) = address (list[lower_bound]) 
+ ((k-lower_bound) * element_size)
Accessing Multi-dimensioned Arrays a[ 0, 0 ] 
a[ 0, 1 ] 
a[ 0, 2 ] 
a[ 1, 0 ] 
a[ 1, 1 ] 
a[ 1, 2 ] a[ 0, 0 ] 
a[ 1, 0 ] 
a[ 0, 1 ] 
a[ 1, 1 ] 
a[ 0, 2 ] 
a[ 1, 2 ] 
Row major order – used in most languages 
Column major order – used in Fortran
• General format Location (a[I,j]) = address of a [row_lb,col_lb] + (((I - row_lb) * n) + (j - col_lb)) * element_size 
Accessing Multi-dimensioned Arrays
Compile-Time Descriptors 
Single-dimensioned array 
Multi-dimensional array
Associative Arrays 
•An associative array is an unordered collection of data elements that are indexed by an equal number of values called keys 
–User defined keys must be stored 
•Similar to Map in Scala 
•Design issues: What is the form of references to elements
Associative Arrays in Perl 
•Names begin with %; literals are delimited by parentheses 
%hi_temps = ("Mon" => 77, "Tue" => 79, “Wed” => 65, …); 
•Subscripting is done using braces and keys 
$hi_temps{"Wed"} = 83; 
–Elements can be removed with delete 
delete $hi_temps{"Tue"};
Record Types 
•A record: 
–heterogeneous aggregate of data elements 
–individual elements are identified by names 
•Popular in most languages, OO languages use objects as records 
•Design issues: 
–What is the syntactic form of references to the field? 
–Are elliptical references allowed
Definition of Records in Ada 
•Record structures are indicated in an orthogonal way 
type Emp_Name_Type is record 
First: String (1..20); 
Mid: String (1..10); 
Last: String (1..20); 
end record; 
type Emp_Record_Type is record 
Emp_Name: Emp_Name_Type; 
Hourly_Rate: Float; 
end record; 
Emp_Rec: Emp_Rec_Type;
References to Records 
•Most language use dot notation 
Emp_Rec.Emp_Name.Mid 
•Fully qualified references must include all record names 
•Elliptical references allow leaving out record names as long as the reference is unambiguous, for example in COBOL 
FIRST, FIRST OF EMP-NAME, and FIRST OF EMP-REC are elliptical references to the employee’s first name
Operations on Records 
•Assignment is very common if the types are identical 
•Ada allows record comparison 
•Ada records can be initialized with aggregate literals 
•COBOL provides MOVE CORRESPONDING 
–Copies fields which have the same name
•Straight forward and safe design 
•Arrays are used when all data values have the same type and/or are processed in the same way 
•Records are opposite 
•Arrays: dynamic subscripting 
•Records: static subscripting 
Evaluation
Offset address relative to the beginning of the records is associated with each field 
Implementation of Record Type
Unions Types 
•A union is a type whose variables are allowed to store different type values at different times during execution 
•Design issues 
–Should type checking be required? 
–Should unions be embedded in records?
Discriminated vs. Free Unions 
•Fortran, C, and C++ provide union constructs in which there is no language support for type checking; the union in these languages is called free union 
•Type checking of unions require that each union include a type indicator called a discriminant 
–Supported by Ada
Ada Union Types 
type Shape is (Circle, Triangle, Rectangle); 
type Colors is (Red, Green, Blue); 
type Figure (Form: Shape) is record 
Filled: Boolean; 
Color: Colors; 
case Form is 
when Circle => Diameter: Float; 
when Triangle => 
Leftside, Rightside: Integer; 
Angle: Float; 
when Rectangle => Side1, Side2: Integer; 
end case; 
end record;
Ada Union Type Illustrated 
A discriminated union of three shape variables
Evaluation of Unions 
•Potentially unsafe construct 
–Do not allow type checking 
•Java and C# do not support unions 
–Reflective of growing concerns for safety in programming language
Pointer and Reference Types 
•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)
Design Issues of Pointers 
•What are the scope of and lifetime of a pointer variable? 
•What is the lifetime of a heap-dynamic variable? 
•Are pointers restricted as to the type of value to which they can point? 
•Are pointers used for dynamic storage management, indirect addressing, or both? 
•Should the language support pointer types, reference types, or both?
Pointer Operations 
•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 * 
j = *ptr 
sets j to the value located at ptr
Pointer Assignment Illustrated 
The assignment operation j = *ptr
Pointer Operations 
•Pointer points to a record in C/C++ 
–Explicit: (*p).name 
–Implicit: p -> name 
•Management of heap use explicit allocation 
–C: subprogram malloc 
–C++: new and delete operators
Problems with Pointers 
•Dangling pointers (dangerous) 
–A pointer points to a heap-dynamic variable that has been de-allocated 
•Lost heap-dynamic variable 
–An allocated heap-dynamic variable that is no longer accessible to the user program (often called garbage)
Pointers in Ada 
•access types 
•Some dangling pointers are disallowed because dynamic objects can be automatically de-allocated at the end of pointer's type scope 
•The lost heap-dynamic variable problem is not eliminated by Ada
Pointers in C and C++ 
int *ptr; 
int count, init; 
... 
ptr = &init; 
count = *ptr; 
•Extremely flexible but must be used with care 
•Pointers can point at any variable regardless of when it was allocated 
•Used for dynamic storage management and addressing
Pointers in C and C++ 
•Pointer arithmetic is possible 
int list [10]; int *ptr; ptr = list; 
*(ptr + 1) 
*(ptr + index) 
*ptr[index] 
•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)
•C++ includes a special kind of pointer type called a reference type that is used primarily for formal parameters 
•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++ 
Reference Types
Evaluation of Pointers 
•Dangling pointers and dangling objects are problems as is heap management 
•Pointers are like goto's--they widen the range of cells that can be accessed by a variable 
•Essential in some kinds of programming applications, e.g. device drivers 
•Using references provide some of the flexibility and capabilities of pointers, without the hazards
Representations of Pointers 
•Large computers use single values 
•Intel microprocessors use segment and offset
Dangling Pointer Problem 
•Tombstone: extra heap cell that is a pointer to the heap-dynamic variable 
–The actual pointer variable points only at tombstones 
–When heap-dynamic variable de-allocated, tombstone remains but set to nil 
–Costly in time and space 
•Locks-and-keys: Pointer values are represented as (key, address) pairs 
–Heap-dynamic variables are represented as variable plus cell for integer lock value 
–When heap-dynamic variable allocated, lock value is created and placed in lock cell and key cell of pointer
Type Checking 
•Generalize the concept of operands and operators to include subprograms and assignments 
•Type checking is the activity of ensuring that the operands of an operator are of compatible types 
•A compatible type is one that is either legal for the operator, or is allowed under language rules to be implicitly converted by compiler- generated code to a legal type 
–This automatic conversion is called a coercion. 
•A type error is the application of an operator to an operand of an inappropriate type
Type Checking (continued) 
•If all type bindings are static, nearly all type checking can be static 
•If type bindings are dynamic, type checking must be dynamic 
•A programming language is strongly typed if type errors are always detected
Strong Typing 
•Advantage of strong typing: allows the detection of the misuses of variables that result in type errors 
•Language examples: 
–FORTRAN 77 is not: EQUIVALENCE 
–C and C++ are not: unions are not type checked 
–Java, C#: strongly typed
Strong Typing (continued) 
•Coercion rules strongly affect strong typing-- they can weaken it considerably (C++ versus Ada) 
•Although Java has just half the assignment coercions of C++, its strong typing is still far less effective than that of Ada
Name Type Equivalence 
•Name type equivalence means the two variables have compatible types if they are in either the same declaration or in declarations that use the same type name 
•Easy to implement but highly restrictive: 
–Subranges of integer types are not compatible with integer types 
–Formal parameters must be the same type as their corresponding actual parameters (Pascal)
Structure Type Equivalence 
•Structure type equivalence means that two variables have equivalent types if their types have identical structures 
•More flexible, but harder to implement
Structure Type Equivalence 
•Consider the problem of two structured types: 
–Are two record types equivalent if they are structurally the same but use different field names? 
–Are two array types equivalent if they are the same except that the subscripts are different? 
(e.g. [1..10] and [0..9]) 
–Are two enumeration types equivalent if their components are spelled differently? 
–With structural type equivalence, you cannot differentiate between types of the same structure (e.g. different units of speed, both float)
Summary 
•The data types of a language are a large part of what determines that language’s style and usefulness 
•The primitive data types of most imperative languages include numeric, character, and Boolean types 
•The user-defined enumeration and subrange types are convenient and add to the readability and reliability of programs 
•Arrays and records are included in most languages 
•Pointers are used for addressing flexibility and to control dynamic storage management 
•Strong typing means detecting all type errors

More Related Content

What's hot

Abstract data types
Abstract data typesAbstract data types
Abstract data types
Luis Goldster
 
Java threads
Java threadsJava threads
Java threads
Prabhakaran V M
 
Data Types, Variables, and Operators
Data Types, Variables, and OperatorsData Types, Variables, and Operators
Data Types, Variables, and Operators
Marwa Ali Eissa
 
PYTHON-Chapter 3-Classes and Object-oriented Programming: MAULIK BORSANIYA
PYTHON-Chapter 3-Classes and Object-oriented Programming: MAULIK BORSANIYAPYTHON-Chapter 3-Classes and Object-oriented Programming: MAULIK BORSANIYA
PYTHON-Chapter 3-Classes and Object-oriented Programming: MAULIK BORSANIYA
Maulik Borsaniya
 
Python numbers
Python numbersPython numbers
Network programming in java - PPT
Network programming in java - PPTNetwork programming in java - PPT
Network programming in java - PPT
kamal kotecha
 
Presentation on Elementary data structures
Presentation on Elementary data structuresPresentation on Elementary data structures
Presentation on Elementary data structures
Kuber Chandra
 
Arrays in python
Arrays in pythonArrays in python
Arrays in python
moazamali28
 
Data structures and algorithms
Data structures and algorithmsData structures and algorithms
Data structures and algorithms
Julie Iskander
 
Abstract data types
Abstract data typesAbstract data types
Abstract data types
Poojith Chowdhary
 
Algorithms Lecture 1: Introduction to Algorithms
Algorithms Lecture 1: Introduction to AlgorithmsAlgorithms Lecture 1: Introduction to Algorithms
Algorithms Lecture 1: Introduction to Algorithms
Mohamed Loey
 
Modules and packages in python
Modules and packages in pythonModules and packages in python
Modules and packages in python
TMARAGATHAM
 
Chapter 05 classes and objects
Chapter 05 classes and objectsChapter 05 classes and objects
Chapter 05 classes and objects
Praveen M Jigajinni
 
Data structures using c
Data structures using cData structures using c
Data structures using c
Prof. Dr. K. Adisesha
 
Algorithms Lecture 2: Analysis of Algorithms I
Algorithms Lecture 2: Analysis of Algorithms IAlgorithms Lecture 2: Analysis of Algorithms I
Algorithms Lecture 2: Analysis of Algorithms I
Mohamed Loey
 
Python dictionary
Python   dictionaryPython   dictionary
Python dictionary
Mohammed Sikander
 
Object Oriented Programming
Object Oriented ProgrammingObject Oriented Programming
Object Oriented Programming
Iqra khalil
 
List in Python
List in PythonList in Python
List in Python
Siddique Ibrahim
 
Strings
StringsStrings
Strings
Mitali Chugh
 
Networking in Java
Networking in JavaNetworking in Java
Networking in Java
Tushar B Kute
 

What's hot (20)

Abstract data types
Abstract data typesAbstract data types
Abstract data types
 
Java threads
Java threadsJava threads
Java threads
 
Data Types, Variables, and Operators
Data Types, Variables, and OperatorsData Types, Variables, and Operators
Data Types, Variables, and Operators
 
PYTHON-Chapter 3-Classes and Object-oriented Programming: MAULIK BORSANIYA
PYTHON-Chapter 3-Classes and Object-oriented Programming: MAULIK BORSANIYAPYTHON-Chapter 3-Classes and Object-oriented Programming: MAULIK BORSANIYA
PYTHON-Chapter 3-Classes and Object-oriented Programming: MAULIK BORSANIYA
 
Python numbers
Python numbersPython numbers
Python numbers
 
Network programming in java - PPT
Network programming in java - PPTNetwork programming in java - PPT
Network programming in java - PPT
 
Presentation on Elementary data structures
Presentation on Elementary data structuresPresentation on Elementary data structures
Presentation on Elementary data structures
 
Arrays in python
Arrays in pythonArrays in python
Arrays in python
 
Data structures and algorithms
Data structures and algorithmsData structures and algorithms
Data structures and algorithms
 
Abstract data types
Abstract data typesAbstract data types
Abstract data types
 
Algorithms Lecture 1: Introduction to Algorithms
Algorithms Lecture 1: Introduction to AlgorithmsAlgorithms Lecture 1: Introduction to Algorithms
Algorithms Lecture 1: Introduction to Algorithms
 
Modules and packages in python
Modules and packages in pythonModules and packages in python
Modules and packages in python
 
Chapter 05 classes and objects
Chapter 05 classes and objectsChapter 05 classes and objects
Chapter 05 classes and objects
 
Data structures using c
Data structures using cData structures using c
Data structures using c
 
Algorithms Lecture 2: Analysis of Algorithms I
Algorithms Lecture 2: Analysis of Algorithms IAlgorithms Lecture 2: Analysis of Algorithms I
Algorithms Lecture 2: Analysis of Algorithms I
 
Python dictionary
Python   dictionaryPython   dictionary
Python dictionary
 
Object Oriented Programming
Object Oriented ProgrammingObject Oriented Programming
Object Oriented Programming
 
List in Python
List in PythonList in Python
List in Python
 
Strings
StringsStrings
Strings
 
Networking in Java
Networking in JavaNetworking in Java
Networking in Java
 

Viewers also liked

2. hadoop
2. hadoop2. hadoop
2. hadoop
Chiou-Nan Chen
 
Primitive data types
Primitive data typesPrimitive data types
Primitive data types
Student
 
09 implementing+subprograms
09 implementing+subprograms09 implementing+subprograms
09 implementing+subprograms
baran19901990
 
Hadoop
HadoopHadoop
Hadoop
Saeed Iqbal
 
How to build a news website use CMS wordpress
How to build a news website use CMS wordpressHow to build a news website use CMS wordpress
How to build a news website use CMS wordpress
baran19901990
 
Introduction to HBase
Introduction to HBaseIntroduction to HBase
Introduction to HBase
Byeongweon Moon
 
08 subprograms
08 subprograms08 subprograms
08 subprograms
baran19901990
 
Untitled Presentation
Untitled PresentationUntitled Presentation
Untitled Presentationbaran19901990
 
Chapter2
Chapter2Chapter2
Chapter2
GF Cleiton
 
Nhập môn công tác kỹ sư
Nhập môn công tác kỹ sưNhập môn công tác kỹ sư
Nhập môn công tác kỹ sư
baran19901990
 
Config websocket on apache
Config websocket on apacheConfig websocket on apache
Config websocket on apache
baran19901990
 
Control structure
Control structureControl structure
Control structure
baran19901990
 
Memory allocation
Memory allocationMemory allocation
Memory allocation
sanya6900
 
Chapter 9 & chapter 10 solutions
Chapter 9 & chapter 10 solutionsChapter 9 & chapter 10 solutions
Chapter 9 & chapter 10 solutions
Saeed Iqbal
 
Chapter 17 dccn
Chapter 17 dccnChapter 17 dccn
Chapter 17 dccn
Hareem Aslam
 
Scope - Static and Dynamic
Scope - Static and DynamicScope - Static and Dynamic
Scope - Static and Dynamic
Sneh Pahilwani
 
Computer Fundamentals Chapter 12 cl
Computer Fundamentals Chapter 12 clComputer Fundamentals Chapter 12 cl
Computer Fundamentals Chapter 12 cl
Saumya Sahu
 
10 logic+programming+with+prolog
10 logic+programming+with+prolog10 logic+programming+with+prolog
10 logic+programming+with+prolog
baran19901990
 
Unit 5
Unit 5Unit 5
Data types
Data typesData types
Data types
Zahid Hussain
 

Viewers also liked (20)

2. hadoop
2. hadoop2. hadoop
2. hadoop
 
Primitive data types
Primitive data typesPrimitive data types
Primitive data types
 
09 implementing+subprograms
09 implementing+subprograms09 implementing+subprograms
09 implementing+subprograms
 
Hadoop
HadoopHadoop
Hadoop
 
How to build a news website use CMS wordpress
How to build a news website use CMS wordpressHow to build a news website use CMS wordpress
How to build a news website use CMS wordpress
 
Introduction to HBase
Introduction to HBaseIntroduction to HBase
Introduction to HBase
 
08 subprograms
08 subprograms08 subprograms
08 subprograms
 
Untitled Presentation
Untitled PresentationUntitled Presentation
Untitled Presentation
 
Chapter2
Chapter2Chapter2
Chapter2
 
Nhập môn công tác kỹ sư
Nhập môn công tác kỹ sưNhập môn công tác kỹ sư
Nhập môn công tác kỹ sư
 
Config websocket on apache
Config websocket on apacheConfig websocket on apache
Config websocket on apache
 
Control structure
Control structureControl structure
Control structure
 
Memory allocation
Memory allocationMemory allocation
Memory allocation
 
Chapter 9 & chapter 10 solutions
Chapter 9 & chapter 10 solutionsChapter 9 & chapter 10 solutions
Chapter 9 & chapter 10 solutions
 
Chapter 17 dccn
Chapter 17 dccnChapter 17 dccn
Chapter 17 dccn
 
Scope - Static and Dynamic
Scope - Static and DynamicScope - Static and Dynamic
Scope - Static and Dynamic
 
Computer Fundamentals Chapter 12 cl
Computer Fundamentals Chapter 12 clComputer Fundamentals Chapter 12 cl
Computer Fundamentals Chapter 12 cl
 
10 logic+programming+with+prolog
10 logic+programming+with+prolog10 logic+programming+with+prolog
10 logic+programming+with+prolog
 
Unit 5
Unit 5Unit 5
Unit 5
 
Data types
Data typesData types
Data types
 

Similar to Datatype

chapter 5.ppt
chapter 5.pptchapter 5.ppt
chapter 5.ppt
ThedronBerhanu
 
Ch06Part1.ppt
Ch06Part1.pptCh06Part1.ppt
Ch06Part1.ppt
kavitamittal18
 
Primitive data types in java
Primitive data types in javaPrimitive data types in java
Primitive data types in java
Umamaheshwariv1
 
332 ch07
332 ch07332 ch07
332 ch07
YaQ10
 
pl12ch6.ppt
pl12ch6.pptpl12ch6.ppt
pl12ch6.ppt
BrandonGarcia294013
 
Ch6
Ch6Ch6
Data.ppt
Data.pptData.ppt
Data.ppt
RithikRaj25
 
ch6-Short.ppt eee cse www rrr www qqq rrr ttt
ch6-Short.ppt eee cse www rrr www qqq rrr tttch6-Short.ppt eee cse www rrr www qqq rrr ttt
ch6-Short.ppt eee cse www rrr www qqq rrr ttt
winimag331
 
8. data types
8. data types8. data types
Quarter-2-CH-1.ppt
Quarter-2-CH-1.pptQuarter-2-CH-1.ppt
Quarter-2-CH-1.ppt
TanTan622589
 
Net framework
Net frameworkNet framework
Net framework
Abhishek Mukherjee
 
Avro intro
Avro introAvro intro
Avro intro
Randy Abernethy
 
Python first day
Python first dayPython first day
Python first day
MARISSTELLA2
 
Python first day
Python first dayPython first day
Python first day
farkhand
 
Learning core java
Learning core javaLearning core java
Learning core java
Abhay Bharti
 
6 data types
6 data types6 data types
6 data types
jigeno
 
Chapter 4.pptx
Chapter 4.pptxChapter 4.pptx
Chapter 4.pptx
RanjanaShevkar
 
5variables in c#
5variables in c#5variables in c#
5variables in c#
Sireesh K
 
Java ce241
Java ce241Java ce241
Java ce241
Minal Maniar
 
Scala, Play 2.0 & Cloud Foundry
Scala, Play 2.0 & Cloud FoundryScala, Play 2.0 & Cloud Foundry
Scala, Play 2.0 & Cloud Foundry
Pray Desai
 

Similar to Datatype (20)

chapter 5.ppt
chapter 5.pptchapter 5.ppt
chapter 5.ppt
 
Ch06Part1.ppt
Ch06Part1.pptCh06Part1.ppt
Ch06Part1.ppt
 
Primitive data types in java
Primitive data types in javaPrimitive data types in java
Primitive data types in java
 
332 ch07
332 ch07332 ch07
332 ch07
 
pl12ch6.ppt
pl12ch6.pptpl12ch6.ppt
pl12ch6.ppt
 
Ch6
Ch6Ch6
Ch6
 
Data.ppt
Data.pptData.ppt
Data.ppt
 
ch6-Short.ppt eee cse www rrr www qqq rrr ttt
ch6-Short.ppt eee cse www rrr www qqq rrr tttch6-Short.ppt eee cse www rrr www qqq rrr ttt
ch6-Short.ppt eee cse www rrr www qqq rrr ttt
 
8. data types
8. data types8. data types
8. data types
 
Quarter-2-CH-1.ppt
Quarter-2-CH-1.pptQuarter-2-CH-1.ppt
Quarter-2-CH-1.ppt
 
Net framework
Net frameworkNet framework
Net framework
 
Avro intro
Avro introAvro intro
Avro intro
 
Python first day
Python first dayPython first day
Python first day
 
Python first day
Python first dayPython first day
Python first day
 
Learning core java
Learning core javaLearning core java
Learning core java
 
6 data types
6 data types6 data types
6 data types
 
Chapter 4.pptx
Chapter 4.pptxChapter 4.pptx
Chapter 4.pptx
 
5variables in c#
5variables in c#5variables in c#
5variables in c#
 
Java ce241
Java ce241Java ce241
Java ce241
 
Scala, Play 2.0 & Cloud Foundry
Scala, Play 2.0 & Cloud FoundryScala, Play 2.0 & Cloud Foundry
Scala, Play 2.0 & Cloud Foundry
 

More from baran19901990

Tìm đường đi xe buýt trong TPHCM bằng Google Map
Tìm đường đi xe buýt trong TPHCM bằng Google MapTìm đường đi xe buýt trong TPHCM bằng Google Map
Tìm đường đi xe buýt trong TPHCM bằng Google Map
baran19901990
 
How to install nginx vs unicorn
How to install nginx vs unicornHow to install nginx vs unicorn
How to install nginx vs unicorn
baran19901990
 
Subprogram
SubprogramSubprogram
Subprogram
baran19901990
 
Lexical
LexicalLexical
Lexical
baran19901990
 
Introduction
IntroductionIntroduction
Introduction
baran19901990
 
07 control+structures
07 control+structures07 control+structures
07 control+structures
baran19901990
 
How to install git on ubuntu
How to install git on ubuntuHow to install git on ubuntu
How to install git on ubuntu
baran19901990
 
Ruby notification
Ruby notificationRuby notification
Ruby notification
baran19901990
 
Rails notification
Rails notificationRails notification
Rails notification
baran19901990
 
Linux notification
Linux notificationLinux notification
Linux notification
baran19901990
 
Báo cáo mô hình quản lý khách sạn
Báo cáo mô hình quản lý khách sạnBáo cáo mô hình quản lý khách sạn
Báo cáo mô hình quản lý khách sạnbaran19901990
 
MDA Framework
MDA FrameworkMDA Framework
MDA Framework
baran19901990
 

More from baran19901990 (15)

Tìm đường đi xe buýt trong TPHCM bằng Google Map
Tìm đường đi xe buýt trong TPHCM bằng Google MapTìm đường đi xe buýt trong TPHCM bằng Google Map
Tìm đường đi xe buýt trong TPHCM bằng Google Map
 
How to install nginx vs unicorn
How to install nginx vs unicornHow to install nginx vs unicorn
How to install nginx vs unicorn
 
Subprogram
SubprogramSubprogram
Subprogram
 
Lexical
LexicalLexical
Lexical
 
Introduction
IntroductionIntroduction
Introduction
 
07 control+structures
07 control+structures07 control+structures
07 control+structures
 
How to install git on ubuntu
How to install git on ubuntuHow to install git on ubuntu
How to install git on ubuntu
 
Ruby notification
Ruby notificationRuby notification
Ruby notification
 
Rails notification
Rails notificationRails notification
Rails notification
 
Linux notification
Linux notificationLinux notification
Linux notification
 
Lab4
Lab4Lab4
Lab4
 
Lab5
Lab5Lab5
Lab5
 
09
0909
09
 
Báo cáo mô hình quản lý khách sạn
Báo cáo mô hình quản lý khách sạnBáo cáo mô hình quản lý khách sạn
Báo cáo mô hình quản lý khách sạn
 
MDA Framework
MDA FrameworkMDA Framework
MDA Framework
 

Recently uploaded

manuaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaal
manuaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaalmanuaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaal
manuaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaal
wolfsoftcompanyco
 
不能毕业如何获得(USYD毕业证)悉尼大学毕业证成绩单一比一原版制作
不能毕业如何获得(USYD毕业证)悉尼大学毕业证成绩单一比一原版制作不能毕业如何获得(USYD毕业证)悉尼大学毕业证成绩单一比一原版制作
不能毕业如何获得(USYD毕业证)悉尼大学毕业证成绩单一比一原版制作
bseovas
 
Azure EA Sponsorship - Customer Guide.pdf
Azure EA Sponsorship - Customer Guide.pdfAzure EA Sponsorship - Customer Guide.pdf
Azure EA Sponsorship - Customer Guide.pdf
AanSulistiyo
 
留学学历(UoA毕业证)奥克兰大学毕业证成绩单官方原版办理
留学学历(UoA毕业证)奥克兰大学毕业证成绩单官方原版办理留学学历(UoA毕业证)奥克兰大学毕业证成绩单官方原版办理
留学学历(UoA毕业证)奥克兰大学毕业证成绩单官方原版办理
bseovas
 
假文凭国外(Adelaide毕业证)澳大利亚国立大学毕业证成绩单办理
假文凭国外(Adelaide毕业证)澳大利亚国立大学毕业证成绩单办理假文凭国外(Adelaide毕业证)澳大利亚国立大学毕业证成绩单办理
假文凭国外(Adelaide毕业证)澳大利亚国立大学毕业证成绩单办理
cuobya
 
存档可查的(USC毕业证)南加利福尼亚大学毕业证成绩单制做办理
存档可查的(USC毕业证)南加利福尼亚大学毕业证成绩单制做办理存档可查的(USC毕业证)南加利福尼亚大学毕业证成绩单制做办理
存档可查的(USC毕业证)南加利福尼亚大学毕业证成绩单制做办理
fovkoyb
 
[HUN][hackersuli] Red Teaming alapok 2024
[HUN][hackersuli] Red Teaming alapok 2024[HUN][hackersuli] Red Teaming alapok 2024
[HUN][hackersuli] Red Teaming alapok 2024
hackersuli
 
一比一原版(USYD毕业证)悉尼大学毕业证如何办理
一比一原版(USYD毕业证)悉尼大学毕业证如何办理一比一原版(USYD毕业证)悉尼大学毕业证如何办理
一比一原版(USYD毕业证)悉尼大学毕业证如何办理
k4ncd0z
 
重新申请毕业证书(RMIT毕业证)皇家墨尔本理工大学毕业证成绩单精仿办理
重新申请毕业证书(RMIT毕业证)皇家墨尔本理工大学毕业证成绩单精仿办理重新申请毕业证书(RMIT毕业证)皇家墨尔本理工大学毕业证成绩单精仿办理
重新申请毕业证书(RMIT毕业证)皇家墨尔本理工大学毕业证成绩单精仿办理
vmemo1
 
制作毕业证书(ANU毕业证)莫纳什大学毕业证成绩单官方原版办理
制作毕业证书(ANU毕业证)莫纳什大学毕业证成绩单官方原版办理制作毕业证书(ANU毕业证)莫纳什大学毕业证成绩单官方原版办理
制作毕业证书(ANU毕业证)莫纳什大学毕业证成绩单官方原版办理
cuobya
 
办理毕业证(UPenn毕业证)宾夕法尼亚大学毕业证成绩单快速办理
办理毕业证(UPenn毕业证)宾夕法尼亚大学毕业证成绩单快速办理办理毕业证(UPenn毕业证)宾夕法尼亚大学毕业证成绩单快速办理
办理毕业证(UPenn毕业证)宾夕法尼亚大学毕业证成绩单快速办理
uehowe
 
Should Repositories Participate in the Fediverse?
Should Repositories Participate in the Fediverse?Should Repositories Participate in the Fediverse?
Should Repositories Participate in the Fediverse?
Paul Walk
 
办理新西兰奥克兰大学毕业证学位证书范本原版一模一样
办理新西兰奥克兰大学毕业证学位证书范本原版一模一样办理新西兰奥克兰大学毕业证学位证书范本原版一模一样
办理新西兰奥克兰大学毕业证学位证书范本原版一模一样
xjq03c34
 
Design Thinking NETFLIX using all techniques.pptx
Design Thinking NETFLIX using all techniques.pptxDesign Thinking NETFLIX using all techniques.pptx
Design Thinking NETFLIX using all techniques.pptx
saathvikreddy2003
 
Discover the benefits of outsourcing SEO to India
Discover the benefits of outsourcing SEO to IndiaDiscover the benefits of outsourcing SEO to India
Discover the benefits of outsourcing SEO to India
davidjhones387
 
制作原版1:1(Monash毕业证)莫纳什大学毕业证成绩单办理假
制作原版1:1(Monash毕业证)莫纳什大学毕业证成绩单办理假制作原版1:1(Monash毕业证)莫纳什大学毕业证成绩单办理假
制作原版1:1(Monash毕业证)莫纳什大学毕业证成绩单办理假
ukwwuq
 
留学挂科(UofM毕业证)明尼苏达大学毕业证成绩单复刻办理
留学挂科(UofM毕业证)明尼苏达大学毕业证成绩单复刻办理留学挂科(UofM毕业证)明尼苏达大学毕业证成绩单复刻办理
留学挂科(UofM毕业证)明尼苏达大学毕业证成绩单复刻办理
uehowe
 
HijackLoader Evolution: Interactive Process Hollowing
HijackLoader Evolution: Interactive Process HollowingHijackLoader Evolution: Interactive Process Hollowing
HijackLoader Evolution: Interactive Process Hollowing
Donato Onofri
 
Search Result Showing My Post is Now Buried
Search Result Showing My Post is Now BuriedSearch Result Showing My Post is Now Buried
Search Result Showing My Post is Now Buried
Trish Parr
 
办理毕业证(NYU毕业证)纽约大学毕业证成绩单官方原版办理
办理毕业证(NYU毕业证)纽约大学毕业证成绩单官方原版办理办理毕业证(NYU毕业证)纽约大学毕业证成绩单官方原版办理
办理毕业证(NYU毕业证)纽约大学毕业证成绩单官方原版办理
uehowe
 

Recently uploaded (20)

manuaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaal
manuaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaalmanuaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaal
manuaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaal
 
不能毕业如何获得(USYD毕业证)悉尼大学毕业证成绩单一比一原版制作
不能毕业如何获得(USYD毕业证)悉尼大学毕业证成绩单一比一原版制作不能毕业如何获得(USYD毕业证)悉尼大学毕业证成绩单一比一原版制作
不能毕业如何获得(USYD毕业证)悉尼大学毕业证成绩单一比一原版制作
 
Azure EA Sponsorship - Customer Guide.pdf
Azure EA Sponsorship - Customer Guide.pdfAzure EA Sponsorship - Customer Guide.pdf
Azure EA Sponsorship - Customer Guide.pdf
 
留学学历(UoA毕业证)奥克兰大学毕业证成绩单官方原版办理
留学学历(UoA毕业证)奥克兰大学毕业证成绩单官方原版办理留学学历(UoA毕业证)奥克兰大学毕业证成绩单官方原版办理
留学学历(UoA毕业证)奥克兰大学毕业证成绩单官方原版办理
 
假文凭国外(Adelaide毕业证)澳大利亚国立大学毕业证成绩单办理
假文凭国外(Adelaide毕业证)澳大利亚国立大学毕业证成绩单办理假文凭国外(Adelaide毕业证)澳大利亚国立大学毕业证成绩单办理
假文凭国外(Adelaide毕业证)澳大利亚国立大学毕业证成绩单办理
 
存档可查的(USC毕业证)南加利福尼亚大学毕业证成绩单制做办理
存档可查的(USC毕业证)南加利福尼亚大学毕业证成绩单制做办理存档可查的(USC毕业证)南加利福尼亚大学毕业证成绩单制做办理
存档可查的(USC毕业证)南加利福尼亚大学毕业证成绩单制做办理
 
[HUN][hackersuli] Red Teaming alapok 2024
[HUN][hackersuli] Red Teaming alapok 2024[HUN][hackersuli] Red Teaming alapok 2024
[HUN][hackersuli] Red Teaming alapok 2024
 
一比一原版(USYD毕业证)悉尼大学毕业证如何办理
一比一原版(USYD毕业证)悉尼大学毕业证如何办理一比一原版(USYD毕业证)悉尼大学毕业证如何办理
一比一原版(USYD毕业证)悉尼大学毕业证如何办理
 
重新申请毕业证书(RMIT毕业证)皇家墨尔本理工大学毕业证成绩单精仿办理
重新申请毕业证书(RMIT毕业证)皇家墨尔本理工大学毕业证成绩单精仿办理重新申请毕业证书(RMIT毕业证)皇家墨尔本理工大学毕业证成绩单精仿办理
重新申请毕业证书(RMIT毕业证)皇家墨尔本理工大学毕业证成绩单精仿办理
 
制作毕业证书(ANU毕业证)莫纳什大学毕业证成绩单官方原版办理
制作毕业证书(ANU毕业证)莫纳什大学毕业证成绩单官方原版办理制作毕业证书(ANU毕业证)莫纳什大学毕业证成绩单官方原版办理
制作毕业证书(ANU毕业证)莫纳什大学毕业证成绩单官方原版办理
 
办理毕业证(UPenn毕业证)宾夕法尼亚大学毕业证成绩单快速办理
办理毕业证(UPenn毕业证)宾夕法尼亚大学毕业证成绩单快速办理办理毕业证(UPenn毕业证)宾夕法尼亚大学毕业证成绩单快速办理
办理毕业证(UPenn毕业证)宾夕法尼亚大学毕业证成绩单快速办理
 
Should Repositories Participate in the Fediverse?
Should Repositories Participate in the Fediverse?Should Repositories Participate in the Fediverse?
Should Repositories Participate in the Fediverse?
 
办理新西兰奥克兰大学毕业证学位证书范本原版一模一样
办理新西兰奥克兰大学毕业证学位证书范本原版一模一样办理新西兰奥克兰大学毕业证学位证书范本原版一模一样
办理新西兰奥克兰大学毕业证学位证书范本原版一模一样
 
Design Thinking NETFLIX using all techniques.pptx
Design Thinking NETFLIX using all techniques.pptxDesign Thinking NETFLIX using all techniques.pptx
Design Thinking NETFLIX using all techniques.pptx
 
Discover the benefits of outsourcing SEO to India
Discover the benefits of outsourcing SEO to IndiaDiscover the benefits of outsourcing SEO to India
Discover the benefits of outsourcing SEO to India
 
制作原版1:1(Monash毕业证)莫纳什大学毕业证成绩单办理假
制作原版1:1(Monash毕业证)莫纳什大学毕业证成绩单办理假制作原版1:1(Monash毕业证)莫纳什大学毕业证成绩单办理假
制作原版1:1(Monash毕业证)莫纳什大学毕业证成绩单办理假
 
留学挂科(UofM毕业证)明尼苏达大学毕业证成绩单复刻办理
留学挂科(UofM毕业证)明尼苏达大学毕业证成绩单复刻办理留学挂科(UofM毕业证)明尼苏达大学毕业证成绩单复刻办理
留学挂科(UofM毕业证)明尼苏达大学毕业证成绩单复刻办理
 
HijackLoader Evolution: Interactive Process Hollowing
HijackLoader Evolution: Interactive Process HollowingHijackLoader Evolution: Interactive Process Hollowing
HijackLoader Evolution: Interactive Process Hollowing
 
Search Result Showing My Post is Now Buried
Search Result Showing My Post is Now BuriedSearch Result Showing My Post is Now Buried
Search Result Showing My Post is Now Buried
 
办理毕业证(NYU毕业证)纽约大学毕业证成绩单官方原版办理
办理毕业证(NYU毕业证)纽约大学毕业证成绩单官方原版办理办理毕业证(NYU毕业证)纽约大学毕业证成绩单官方原版办理
办理毕业证(NYU毕业证)纽约大学毕业证成绩单官方原版办理
 

Datatype

  • 1. Chapter 6: Data Types Principles of Programming Languages
  • 2. Contents •Primitive Data Types •Character String Types •User-Defined Ordinal Types •Array Types •Associative Arrays •Record Types •Union Types •Pointer and Reference Types •Type Checking
  • 3. Introduction •A data type defines –a collection of data objects –a set of predefined operations •Abstract Data Type: Interface (visible) are separated from the representation and operation (hidden) •Uses of type system: –Error detection –Program modularization assistant –Documentation
  • 4. The Next Part •Primitive Data Types •Character String Types •User-Defined Ordinal Types •Array Types •Associative Arrays •Record Types •Union Types •Pointer and Reference Types •Type Checking
  • 5. Primitive Data Types •Data types that are not defined in terms of other data types •Some primitive data types are merely reflections of the hardware •Others require little non-hardware support
  • 6. Primitive Data Types: Integer •Languages may support several sizes of integer –Java’s signed integer sizes: byte, short, int, long •Some languages include unsigned integers •Supported directly by hardware: a string of bits •To represent negative numbers: twos complement
  • 7. Primitive Data Types: Floating Point •Model real numbers, but only as approximations •Languages for scientific use support at least two floating-point types (e.g., float and double) •Precision and range •IEEE Floating-Point Standard 754
  • 8. IEEE 754 Sign bit Exponent Fraction 23 bits 8 bits Single-pricision Sign bit Exponent Fraction 52 bits 11 bits Double-pricision
  • 9. Primitive Data Types: Decimal •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
  • 10. Primitive Data Types: Boolean •Simplest of all •Range of values: two elements, one for “true” and one for “false” •Could be implemented as bits, but often as bytes –Advantage: readability
  • 11. Primitive Data Types: 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
  • 12. Character String Types •Values are sequences of characters •Design issues: –Is it a primitive type or just a special kind of array? –Should the length of strings be static or dynamic?
  • 13. Character String Types Operations •Typical operations: –Assignment –Comparison (=, >, etc.) –Catenation –Substring reference –Pattern matching •Regular Expression
  • 14. •C and C++ –Not primitive –Use char arrays and a library of functions that provide operations •Java –Primitive via the String class –Immutable Character String in Some Languages
  • 15. Character String Length Options •Static: Python, Java’s String class •Limited Dynamic Length: C –In C, a special character is used to indicate the end of a string’s characters, rather than maintaining the length •Dynamic (no maximum): Perl, JavaScript, standard C++ library •Ada supports all three string length options
  • 16. Character String Type Evaluation •Aid to writability •As a primitive type with static length, they are inexpensive to provide--why not have them? •Dynamic length is nice, but is it worth the expense?
  • 17. 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
  • 18. Compile-time descriptor for static strings Run-time descriptor for limited dynamic strings Descriptor
  • 19. 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
  • 20. 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}; days myDay = Mon, yourDay = Tue; •Design issues: –Is an enumeration constant allowed to appear in more than one type definition? –Are enumeration values coerced to integer? –Are any other types coerced to an enumeration type?
  • 21. Evaluation of Enumerated Type •Readability –no need to code a color as a number •Reliability –Ada, C# and Java 5.0 •operations (don’t allow colors to be added) •No enumeration variable can be assigned a value outside its defined range •Better support for enumeration than C++: enumeration type variables are not coerced into integer types
  • 22. •Enumeration types are implemented as integers Implementation of User-Defined Ordinal Types
  • 23. The Next Part •Primitive Data Types •Character String Types •User-Defined Ordinal Types •Array Types •Associative Arrays •Record Types •Union Types •Pointer and Reference Types •Type Checking
  • 24. Array Types •Collection of homogeneous data elements •Each element is identified by its position relative to the first element •Homogeneous: data elements are of same type •Referenced using subscript expression
  • 25. Array Indexing •Indexing (or subscripting) is a mapping from indices to elements array_name (index_value_list)  an element •Index Syntax –Fortran, Ada use parentheses –Most other languages use brackets
  • 26. Arrays Index (Subscript) Types •What type are legal for subscripts? •Pascal, Ada: any ordinal type (integer, boolean, char, enumeration) •Others: subrange of integers •Are subscripting expressions range checked? •Most contemporary languages do not specify range checking but Java, ML, C# •Unusual case: Perl
  • 27. •Static: subscript ranges are statically bound and storage allocation is static –Advantage: efficiency (no dynamic allocation) –Disadvantage: storage is bound all the time •Fixed stack-dynamic: subscript ranges are statically bound, but the allocation is done at declaration time –Advantage: space efficiency –Disadvantage: dynamic allocation Subscript Binding & Array Categories
  • 28. •Stack-dynamic: subscript ranges are dynamically bound and the storage allocation is dynamic (done at run-time) –Advantage: flexibility (the size of an array need not be known until the array is to be used) •Fixed heap-dynamic: similar to fixed stack- dynamic: storage binding is dynamic but fixed after allocation (i.e., binding is done when requested and storage is allocated from heap, not stack) Subscript Binding & Array Categories
  • 29. •Heap-dynamic: binding of subscript ranges and storage allocation is dynamic and can change any number of times –Advantage: flexibility (arrays can grow or shrink during program execution) Subscript Binding & Array Categories
  • 30. Subscript Binding & Array Categories •C and C++ arrays that include static modifier are static •C and C++ arrays without static modifier are fixed stack-dynamic •Ada arrays can be stack-dynamic Get(List_Len); declare List : array (1..List_Len) of Integer; begin ... end;
  • 31. Subscript Binding & Array Categories •C and C++ provide fixed heap-dynamic arrays –malloc and free, new and delete •All arrays in Java are fixed heap-dynamic •C# includes a array class ArrayList that provides heap-dynamic ArrayList intList = new ArrayList(); intList.Add(nextOne); •Perl and JavaScript support heap-dynamic arrays
  • 32. 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"};
  • 33. Arrays Operations •Assignment, catenation, comparison for equality, and slices •C-based languages do not provide any array operations •Java, C++ and C# use methods to manipulate
  • 34. Rectangular and Jagged Arrays •C, C++, Java, C#: jagged arrays myArray[3][7] •Fortran, Ada, C#: rectangular array myArray[3,7] rectangular jagged
  • 35. Slices •A slice is some substructure of an array; nothing more than a referencing mechanism •Slices are only useful in languages that have array operations •E.g. Python vector = [2, 4, 6, 8, 10, 12, 14, 16] mat = [[1, 2, 3],[4, 5, 6],[7, 8, 9]] vector[3:6], mat[1], mat[0][0:2]
  • 36. Implementation of Arrays •Access function maps subscript expressions to an address in the array •Single-dimensioned: list of adjacent memory cells •Access function for single-dimensioned arrays: address(list[k]) = address (list[lower_bound]) + ((k-lower_bound) * element_size)
  • 37. Accessing Multi-dimensioned Arrays a[ 0, 0 ] a[ 0, 1 ] a[ 0, 2 ] a[ 1, 0 ] a[ 1, 1 ] a[ 1, 2 ] a[ 0, 0 ] a[ 1, 0 ] a[ 0, 1 ] a[ 1, 1 ] a[ 0, 2 ] a[ 1, 2 ] Row major order – used in most languages Column major order – used in Fortran
  • 38. • General format Location (a[I,j]) = address of a [row_lb,col_lb] + (((I - row_lb) * n) + (j - col_lb)) * element_size Accessing Multi-dimensioned Arrays
  • 39. Compile-Time Descriptors Single-dimensioned array Multi-dimensional array
  • 40. Associative Arrays •An associative array is an unordered collection of data elements that are indexed by an equal number of values called keys –User defined keys must be stored •Similar to Map in Scala •Design issues: What is the form of references to elements
  • 41. Associative Arrays in Perl •Names begin with %; literals are delimited by parentheses %hi_temps = ("Mon" => 77, "Tue" => 79, “Wed” => 65, …); •Subscripting is done using braces and keys $hi_temps{"Wed"} = 83; –Elements can be removed with delete delete $hi_temps{"Tue"};
  • 42. Record Types •A record: –heterogeneous aggregate of data elements –individual elements are identified by names •Popular in most languages, OO languages use objects as records •Design issues: –What is the syntactic form of references to the field? –Are elliptical references allowed
  • 43. Definition of Records in Ada •Record structures are indicated in an orthogonal way type Emp_Name_Type is record First: String (1..20); Mid: String (1..10); Last: String (1..20); end record; type Emp_Record_Type is record Emp_Name: Emp_Name_Type; Hourly_Rate: Float; end record; Emp_Rec: Emp_Rec_Type;
  • 44. References to Records •Most language use dot notation Emp_Rec.Emp_Name.Mid •Fully qualified references must include all record names •Elliptical references allow leaving out record names as long as the reference is unambiguous, for example in COBOL FIRST, FIRST OF EMP-NAME, and FIRST OF EMP-REC are elliptical references to the employee’s first name
  • 45. Operations on Records •Assignment is very common if the types are identical •Ada allows record comparison •Ada records can be initialized with aggregate literals •COBOL provides MOVE CORRESPONDING –Copies fields which have the same name
  • 46. •Straight forward and safe design •Arrays are used when all data values have the same type and/or are processed in the same way •Records are opposite •Arrays: dynamic subscripting •Records: static subscripting Evaluation
  • 47. Offset address relative to the beginning of the records is associated with each field Implementation of Record Type
  • 48. Unions Types •A union is a type whose variables are allowed to store different type values at different times during execution •Design issues –Should type checking be required? –Should unions be embedded in records?
  • 49. Discriminated vs. Free Unions •Fortran, C, and C++ provide union constructs in which there is no language support for type checking; the union in these languages is called free union •Type checking of unions require that each union include a type indicator called a discriminant –Supported by Ada
  • 50. Ada Union Types type Shape is (Circle, Triangle, Rectangle); type Colors is (Red, Green, Blue); type Figure (Form: Shape) is record Filled: Boolean; Color: Colors; case Form is when Circle => Diameter: Float; when Triangle => Leftside, Rightside: Integer; Angle: Float; when Rectangle => Side1, Side2: Integer; end case; end record;
  • 51. Ada Union Type Illustrated A discriminated union of three shape variables
  • 52. Evaluation of Unions •Potentially unsafe construct –Do not allow type checking •Java and C# do not support unions –Reflective of growing concerns for safety in programming language
  • 53. Pointer and Reference Types •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)
  • 54. Design Issues of Pointers •What are the scope of and lifetime of a pointer variable? •What is the lifetime of a heap-dynamic variable? •Are pointers restricted as to the type of value to which they can point? •Are pointers used for dynamic storage management, indirect addressing, or both? •Should the language support pointer types, reference types, or both?
  • 55. Pointer Operations •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 * j = *ptr sets j to the value located at ptr
  • 56. Pointer Assignment Illustrated The assignment operation j = *ptr
  • 57. Pointer Operations •Pointer points to a record in C/C++ –Explicit: (*p).name –Implicit: p -> name •Management of heap use explicit allocation –C: subprogram malloc –C++: new and delete operators
  • 58. Problems with Pointers •Dangling pointers (dangerous) –A pointer points to a heap-dynamic variable that has been de-allocated •Lost heap-dynamic variable –An allocated heap-dynamic variable that is no longer accessible to the user program (often called garbage)
  • 59. Pointers in Ada •access types •Some dangling pointers are disallowed because dynamic objects can be automatically de-allocated at the end of pointer's type scope •The lost heap-dynamic variable problem is not eliminated by Ada
  • 60. Pointers in C and C++ int *ptr; int count, init; ... ptr = &init; count = *ptr; •Extremely flexible but must be used with care •Pointers can point at any variable regardless of when it was allocated •Used for dynamic storage management and addressing
  • 61. Pointers in C and C++ •Pointer arithmetic is possible int list [10]; int *ptr; ptr = list; *(ptr + 1) *(ptr + index) *ptr[index] •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)
  • 62. •C++ includes a special kind of pointer type called a reference type that is used primarily for formal parameters •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++ Reference Types
  • 63. Evaluation of Pointers •Dangling pointers and dangling objects are problems as is heap management •Pointers are like goto's--they widen the range of cells that can be accessed by a variable •Essential in some kinds of programming applications, e.g. device drivers •Using references provide some of the flexibility and capabilities of pointers, without the hazards
  • 64. Representations of Pointers •Large computers use single values •Intel microprocessors use segment and offset
  • 65. Dangling Pointer Problem •Tombstone: extra heap cell that is a pointer to the heap-dynamic variable –The actual pointer variable points only at tombstones –When heap-dynamic variable de-allocated, tombstone remains but set to nil –Costly in time and space •Locks-and-keys: Pointer values are represented as (key, address) pairs –Heap-dynamic variables are represented as variable plus cell for integer lock value –When heap-dynamic variable allocated, lock value is created and placed in lock cell and key cell of pointer
  • 66. Type Checking •Generalize the concept of operands and operators to include subprograms and assignments •Type checking is the activity of ensuring that the operands of an operator are of compatible types •A compatible type is one that is either legal for the operator, or is allowed under language rules to be implicitly converted by compiler- generated code to a legal type –This automatic conversion is called a coercion. •A type error is the application of an operator to an operand of an inappropriate type
  • 67. Type Checking (continued) •If all type bindings are static, nearly all type checking can be static •If type bindings are dynamic, type checking must be dynamic •A programming language is strongly typed if type errors are always detected
  • 68. Strong Typing •Advantage of strong typing: allows the detection of the misuses of variables that result in type errors •Language examples: –FORTRAN 77 is not: EQUIVALENCE –C and C++ are not: unions are not type checked –Java, C#: strongly typed
  • 69. Strong Typing (continued) •Coercion rules strongly affect strong typing-- they can weaken it considerably (C++ versus Ada) •Although Java has just half the assignment coercions of C++, its strong typing is still far less effective than that of Ada
  • 70. Name Type Equivalence •Name type equivalence means the two variables have compatible types if they are in either the same declaration or in declarations that use the same type name •Easy to implement but highly restrictive: –Subranges of integer types are not compatible with integer types –Formal parameters must be the same type as their corresponding actual parameters (Pascal)
  • 71. Structure Type Equivalence •Structure type equivalence means that two variables have equivalent types if their types have identical structures •More flexible, but harder to implement
  • 72. Structure Type Equivalence •Consider the problem of two structured types: –Are two record types equivalent if they are structurally the same but use different field names? –Are two array types equivalent if they are the same except that the subscripts are different? (e.g. [1..10] and [0..9]) –Are two enumeration types equivalent if their components are spelled differently? –With structural type equivalence, you cannot differentiate between types of the same structure (e.g. different units of speed, both float)
  • 73. Summary •The data types of a language are a large part of what determines that language’s style and usefulness •The primitive data types of most imperative languages include numeric, character, and Boolean types •The user-defined enumeration and subrange types are convenient and add to the readability and reliability of programs •Arrays and records are included in most languages •Pointers are used for addressing flexibility and to control dynamic storage management •Strong typing means detecting all type errors