SlideShare a Scribd company logo
1 of 33
ISBN 0-321-33025-0
Chapter 6
Data Types
Copyright © 2006 Addison-Wesley. All rights reserved. 6-2
Chapter 6 Topics
• Introduction
• Primitive Data Types
• Character String Types
• User-Defined Ordinal Types
• Array Types
• Associative Arrays
• Record Types
• Union Types
• Pointer and Reference Types
Copyright © 2006 Addison-Wesley. All rights reserved. 6-3
Introduction
• A data type defines a collection of data
objects and a set of predefined operations
on those objects
• A descriptor is the collection of the
attributes of a variable
• An object represents an instance of a
user-defined (abstract data) type
• One design issue for all data types: What
operations are defined and how are they
specified?
Copyright © 2006 Addison-Wesley. All rights reserved. 6-4
Primitive Data Types
• Almost all programming languages provide
a set of primitive data types
• Primitive data types: Those not defined in
terms of other data types
• Some primitive data types are merely
reflections of the hardware, e.g. integer
types
• Others require a little non-hardware
support for their implementation
Copyright © 2006 Addison-Wesley. All rights reserved. 6-5
Primitive Data Types: Integer
• Almost always an exact reflection of the
hardware so the mapping is trivial
• Java’s signed integer sizes: byte, short,
int, long
• A signed integer value is represented
by a string of bits with usually the
leftmost bit representing the sign.
Type Size(bits) Def. value Min. value Max. value
Byte 8 0 -128 +127
Short 16 0 -32768 +32767
Int 32 0 -2147483648 +2147483647
Long 64 0 -9223372036854775808 +9223372036854775807
Copyright © 2006 Addison-Wesley. All rights reserved. 6-6
Primitive Data Types: Floating Point
• Model real numbers, but only as
approximations, e.g. Pi
• Languages for scientific use support at
least two floating-point types, e.g.,
float(usually stored in 4 bytes of
memory) and double(8 bytes of
memory, provides larger fractional
part);
•
Copyright © 2006 Addison-Wesley. All rights reserved. 6-7
Primitive Data Types: Decimal
• For business applications (money)
– Essential to COBOL
– C# offers a decimal data type
• Store a fixed number of decimal digits, with
the decimal point at a fixed position in the
value, unlike float or double data types
which store approximation many times
• Stored like character string, using binary
codes for the decimal digits
• More information about decimal data type:
– http://msdn2.microsoft.com/enus/library/aa691147(VS.71).aspx
– http://technet.microsoft.com/en-us/library/ms187912.aspx
Copyright © 2006 Addison-Wesley. All rights reserved. 6-8
Primitive Data Types: Decimal
• Advantage: accuracy
• Disadvantages:
– limited range (no exponents are allowed)
– wastes memory, stored one digit per byte,
sometimes two digits per byte.
Copyright © 2006 Addison-Wesley. All rights reserved. 6-9
Primitive Data Types: Boolean
• Simplest of all
• Range of values: two elements, one for
“true” and one for “false”
• C89: all operands with nonzero values are
considered true, and zero is considered
false when used in conditionals.
• C99 and C++ have a Boolean type, but they
also allow numeric expressions to be used
as if they were Boolean. (Not the case in
Java and C#)
• Often used to represent switches or flags
• More readable than using integers
Copyright © 2006 Addison-Wesley. All rights reserved. 6-10
Primitive Data Types: Character
• Stored as numeric codings
• Most commonly used coding: ASCII
– Uses 0 to 127 to code 128 different characters
• An alternative, 16-bit coding: Unicode
– Includes characters from most natural
languages
– First 128 characters are identical to ASCII
– Originally used in Java
– C# and JavaScript also support Unicode
Copyright © 2006 Addison-Wesley. All rights reserved. 6-11
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?
Copyright © 2006 Addison-Wesley. All rights reserved. 6-12
Character String Type in Certain
Languages
• C and C++
– Not primitive
– Use char arrays and a library of functions that
provide operations
– Character strings are terminated with a special
character, null, which is represented with zero.
– The library operations simply carry out their
operations until the null character being
operated on. Library functions that produce
strings often supply the null character.
– Common library functions:
• strcpy, strcat, strcmp, strlen
Copyright © 2006 Addison-Wesley. All rights reserved. 6-13
Character String Type in Certain
Languages
• C and C++
– String manipulation functions of the C standard
library is unsafe, as they don’t guard against
overflowing the destination. E.g.:
strcpy(src, dest);
If the length of dest is 20 and the length of src
is 50, strcpy will write over the 30 bytes that
follow dest, which is often in the run-time
stack.
- C++ programmer should use the string class
from the standard library
Copyright © 2006 Addison-Wesley. All rights reserved. 6-14
Character String Types Operations
• Java:
– Strings are supported as a primitive type by the
String class [string a=“abcd” is the same as
String a = new String(“abcd”)], whose values are
constant strings (each time when the value is
changed, a new String object is created).
– StringBuffer class: values of a string is
changeable.
• C#:
– Similar to Java
• C++:
– C-style strings and strings in its standard class
library which is similar to that of Java
Copyright © 2006 Addison-Wesley. All rights reserved. 6-15
Character String Types Operations
• Pattern matching
– Fundamental character string operation
– Often called regular expressions
– E.g., /[A-Za-z][A-Za-zd]+ matches string that
begin with a letter, followed by one or more
letters or digits
– Perl, JavaScript and PHP have built-in pattern
matching operations
– Java, C++ and C# have pattern matching
capabilities in the class libraries, e.g. Java:
Pattern p = Pattern.compile("a*b");
Matcher m = p.matcher("aaaaab");
boolean b = m.matches();
Copyright © 2006 Addison-Wesley. All rights reserved. 6-16
Character String Length Options
• Static Length: COBOL, Java’s String class
– Length is set when the string is created, fixed length
• Limited Dynamic Length: C and C++
– Length is set to have a maximum, varying length
• Dynamic Length(no maximum): Perl, JavaScript
– No length limit, varying length. Required overhead of
dynamic storage allocation and deallocation but provides
maximum flexibility
• Ada supports all three string length options:
– String: static
– Bounded_String: limited dynamic
– Unbounded_String: dynamic
Copyright © 2006 Addison-Wesley. All rights reserved. 6-17
Character String Type Evaluation
• Aid to writability
• As a primitive type with static length, they
are inexpensive to provide
• Simple pattern matching and catenation are
essential, should be included
• Dynamic length is most flexible, but
overhead of implementation must be
weighed. Often included only in languages
that are interpreted.
Copyright © 2006 Addison-Wesley. All rights reserved. 6-18
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++)
type of string
address of the first character
type of string
address of the first character
Copyright © 2006 Addison-Wesley. All rights reserved. 6-19
Character String Implementation
• Dynamic length: need run-time descriptor;
allocation/de-allocation is the biggest
implementation problem. Two approches:
– String is stored in a linked list, so when a string grows,
the newly required cells can come from anywhere in the
heap. Drawback – extra storage occupied by the links in
the list representation and the necessary complexity of
string operations. But allocation and deallocation process
is simple.
– Store complete strings in adjacent storage cells. When the
storage for the adjacent cell is not available(when the
string grows), a new area of memory is found to store the
complete new string. Faster string operation and requires
less storage, but slower allocation and deallocation
process. This approach is typically used.
Copyright © 2006 Addison-Wesley. All rights reserved. 6-20
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
Copyright © 2006 Addison-Wesley. All rights reserved. 6-21
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};
• The enumeration constants are typically implicitly
assigned the integer values, 0, 1, …, but can be
explicitly assigned any integer literal in the type’s
definition
• Design issues
– Is an enumeration constant allowed to appear in more
than one type definition, and if so, how is the type of an
occurrence of that constant checked?
– Are enumeration values coerced to integer?
– Any other type coerced to an enumeration type?
Copyright © 2006 Addison-Wesley. All rights reserved. 6-22
Design
• In languages that do not have enumeration types,
programmers usually simulate them with integer
values. E.g. Fortran 77, use 0 to represent blue and
1 to represent red:
INTEGER RED, BLUE
DATA RED, BLUE/0,1/
Problem: there is no type checking when they are
used. It would be legal to add two together. Or
they can be assigned any integer value thus
destroying the relationship with the colors.
Copyright © 2006 Addison-Wesley. All rights reserved. 6-23
Design
• In C++, we could have
enum colors {red, blue, green, yellow, black};
colors myColor = blue, yourColor = red;
The enumeration values are coerced to int when
they are put in integer context. E.g. myColor++
would assign green to myColor.
• In Java, all enumeration types are implicitly
subclasses of the predefined class Enum. They can
have instance data fields, constructors and
methods.
Copyright © 2006 Addison-Wesley. All rights reserved. 6-24
Design
– Java Example
Enumeration days;
Vector dayNames = new Vector();
dayNames.add("Monday");
…
dayNames.add("Friday");
days = dayNames.elements();
while (days.hasMoreElements())
System.out.println(days.nextElement());
• C# enumeration types are like those of C++
except that they are never coerced to integer.
Operations are restricted to those that make sense.
The range of values is restricted to that of the
particular enumeration type.
Copyright © 2006 Addison-Wesley. All rights reserved. 6-25
Evaluation of Enumerated Type
• Aid to readability, e.g., no need to code a
color as a number
• Aid to reliability, e.g., compiler can check:
– operations (don’t allow colors to be added)
– No enumeration variable can be assigned a
value outside its defined range, e.g. if the colors
type has 10 enumeration constants and uses 0 ..
9 as its internal values, no number greater than
9 can be assigned to a colors type variable.
– Ada, C#, and Java 5.0 provide better support for
enumeration than C++ because enumeration
type variables in these languages are not
coerced into integer types
Copyright © 2006 Addison-Wesley. All rights reserved. 6-26
Evaluation of Enumerated Type
• C treats enumeration variables like integer
variables; it does not provide either of the two
advantages.
• C++ is better. Numeric values can be assigned to
enumeration type variables only if they are cast to
the type of the assigned variable. Numeric values
are checked to determine in they are in the range
of the internal values. However if the user uses a
wide range of explicitly assigned values, this
checking is not effective. E.g.
– enum colors {red = 1, blue = 100, green = 100000}
A value assigned to a variable of colors type will only be checked
to determine whether it is in the range of 1..100000.
• Java 5.0, C# and Ada are better, as variables are
never coerced to integer types
Copyright © 2006 Addison-Wesley. All rights reserved. 6-27
Subrange Types
• An ordered contiguous subsequence of an
ordinal type
• Not a new type, but a restricted existing
type
– Example: 12..18 is a subrange of integer type
• Ada’s design
type Days is (mon, tue, wed, thu, fri, sat, sun);
subtype Weekdays is Days range mon..fri;
subtype Index is Integer range 1..100;
Day1: Days;
Day2: Weekday;
Day2 := Day1; //legal if Day1 it not sat or sun
Compatible with its parent type.
Copyright © 2006 Addison-Wesley. All rights reserved. 6-28
Subrange Evaluation
• Aid to readability
– Make it clear to the readers that variables of
subrange can store only certain range of values
• Reliability
– Assigning a value to a subrange variable that is
outside the specified range is detected as an
error
Copyright © 2006 Addison-Wesley. All rights reserved. 6-29
Implementation of User-Defined
Ordinal Types
• Enumeration types are implemented as
integers
• Subrange types are implemented like the
parent types with code inserted (by the
compiler) to restrict assignments to
subrange variables
Copyright © 2006 Addison-Wesley. All rights reserved. 6-30
Array Types
• An array is an aggregate of homogeneous
data elements in which an individual
element is identified by its position in the
aggregate, relative to the first element.
Copyright © 2006 Addison-Wesley. All rights reserved. 6-31
Array Design Issues
• What types are legal for subscripts?
• Are subscripting expressions in element
references range checked?
• When are subscript ranges bound?
• When does allocation take place?
• Are ragged or rectangular
multidimensioned arrays allowed, or both?
• Can arrays be initialized when they have
their storage allocated?
• Are any kind of slices allowed?
Copyright © 2006 Addison-Wesley. All rights reserved. 6-32
Array Indexing
• Indexing (or subscripting) is a mapping
from indices to elements
array_name (index_value_list)  an element
• Index Syntax
– FORTRAN, PL/I, Ada use parentheses
• Ada explicitly uses parentheses to show uniformity
between array references and function calls because
both are mappings
– Most other languages use brackets
Copyright © 2006 Addison-Wesley. All rights reserved. 6-33
Arrays Index (Subscript) Types
• FORTRAN, C: integer only
• Pascal: any ordinal type (integer, Boolean,
char, enumeration)
• Ada: integer or enumeration (includes
Boolean and char)
• Java: integer types only
• C, C++, Perl, and Fortran do not specify
range checking
• Java, ML, C# specify range checking

More Related Content

Similar to Ch06Part1.ppt

Similar to Ch06Part1.ppt (20)

chapter 5.ppt
chapter 5.pptchapter 5.ppt
chapter 5.ppt
 
Data.ppt
Data.pptData.ppt
Data.ppt
 
Avro intro
Avro introAvro intro
Avro intro
 
8. data types
8. data types8. data types
8. data types
 
14-types.ppt
14-types.ppt14-types.ppt
14-types.ppt
 
6 data types
6 data types6 data types
6 data types
 
Learn c sharp at amc square learning
Learn c sharp at amc square learningLearn c sharp at amc square learning
Learn c sharp at amc square learning
 
5 Names, bindings,Typechecking and Scopes
5 Names, bindings,Typechecking and Scopes5 Names, bindings,Typechecking and Scopes
5 Names, bindings,Typechecking and Scopes
 
Java platform
Java platformJava platform
Java platform
 
Csharp
CsharpCsharp
Csharp
 
Arrays Java
Arrays JavaArrays Java
Arrays Java
 
Rust All Hands Winter 2011
Rust All Hands Winter 2011Rust All Hands Winter 2011
Rust All Hands Winter 2011
 
Scala, Play 2.0 & Cloud Foundry
Scala, Play 2.0 & Cloud FoundryScala, Play 2.0 & Cloud Foundry
Scala, Play 2.0 & Cloud Foundry
 
INTRODUCTION TO C PROGRAMMING in basic c language
INTRODUCTION TO C PROGRAMMING in basic c languageINTRODUCTION TO C PROGRAMMING in basic c language
INTRODUCTION TO C PROGRAMMING in basic c language
 
C#
C#C#
C#
 
Java Script
Java ScriptJava Script
Java Script
 
Java Script
Java ScriptJava Script
Java Script
 
Net framework
Net frameworkNet framework
Net framework
 
Python first day
Python first dayPython first day
Python first day
 
Python first day
Python first dayPython first day
Python first day
 

More from kavitamittal18

JDBC.ppt database connectivity in java ppt
JDBC.ppt database connectivity in java pptJDBC.ppt database connectivity in java ppt
JDBC.ppt database connectivity in java pptkavitamittal18
 
chapter7.ppt java programming lecture notes
chapter7.ppt java programming lecture noteschapter7.ppt java programming lecture notes
chapter7.ppt java programming lecture noteskavitamittal18
 
09slide.ppt oops classes and objects concept
09slide.ppt oops classes and objects concept09slide.ppt oops classes and objects concept
09slide.ppt oops classes and objects conceptkavitamittal18
 
480 GPS Tech mobile computing presentation
480 GPS Tech mobile computing presentation480 GPS Tech mobile computing presentation
480 GPS Tech mobile computing presentationkavitamittal18
 
gsm-archtecture.ppt mobile computing ppt
gsm-archtecture.ppt mobile computing pptgsm-archtecture.ppt mobile computing ppt
gsm-archtecture.ppt mobile computing pptkavitamittal18
 
ELECTORAL POLITICS KAMAL PPT.pptx
ELECTORAL POLITICS KAMAL PPT.pptxELECTORAL POLITICS KAMAL PPT.pptx
ELECTORAL POLITICS KAMAL PPT.pptxkavitamittal18
 
lecture-a-java-review.ppt
lecture-a-java-review.pptlecture-a-java-review.ppt
lecture-a-java-review.pptkavitamittal18
 

More from kavitamittal18 (16)

JDBC.ppt database connectivity in java ppt
JDBC.ppt database connectivity in java pptJDBC.ppt database connectivity in java ppt
JDBC.ppt database connectivity in java ppt
 
chapter7.ppt java programming lecture notes
chapter7.ppt java programming lecture noteschapter7.ppt java programming lecture notes
chapter7.ppt java programming lecture notes
 
09slide.ppt oops classes and objects concept
09slide.ppt oops classes and objects concept09slide.ppt oops classes and objects concept
09slide.ppt oops classes and objects concept
 
480 GPS Tech mobile computing presentation
480 GPS Tech mobile computing presentation480 GPS Tech mobile computing presentation
480 GPS Tech mobile computing presentation
 
gsm-archtecture.ppt mobile computing ppt
gsm-archtecture.ppt mobile computing pptgsm-archtecture.ppt mobile computing ppt
gsm-archtecture.ppt mobile computing ppt
 
AdHocTutorial.ppt
AdHocTutorial.pptAdHocTutorial.ppt
AdHocTutorial.ppt
 
ELECTORAL POLITICS KAMAL PPT.pptx
ELECTORAL POLITICS KAMAL PPT.pptxELECTORAL POLITICS KAMAL PPT.pptx
ELECTORAL POLITICS KAMAL PPT.pptx
 
java_lect_03-2.ppt
java_lect_03-2.pptjava_lect_03-2.ppt
java_lect_03-2.ppt
 
Input and Output.pptx
Input and Output.pptxInput and Output.pptx
Input and Output.pptx
 
ch11.ppt
ch11.pptch11.ppt
ch11.ppt
 
11.ppt
11.ppt11.ppt
11.ppt
 
Java-operators.ppt
Java-operators.pptJava-operators.ppt
Java-operators.ppt
 
IntroToOOP.ppt
IntroToOOP.pptIntroToOOP.ppt
IntroToOOP.ppt
 
09slide.ppt
09slide.ppt09slide.ppt
09slide.ppt
 
CSL101_Ch1.ppt
CSL101_Ch1.pptCSL101_Ch1.ppt
CSL101_Ch1.ppt
 
lecture-a-java-review.ppt
lecture-a-java-review.pptlecture-a-java-review.ppt
lecture-a-java-review.ppt
 

Recently uploaded

Application of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptxApplication of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptx959SahilShah
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escortsranjana rawat
 
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionSachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionDr.Costas Sachpazis
 
Current Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCLCurrent Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCLDeelipZope
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxJoão Esperancinha
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVRajaP95
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxwendy cai
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )Tsuyoshi Horigome
 
microprocessor 8085 and its interfacing
microprocessor 8085  and its interfacingmicroprocessor 8085  and its interfacing
microprocessor 8085 and its interfacingjaychoudhary37
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCall Girls in Nagpur High Profile
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024hassan khalil
 
Introduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptxIntroduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptxvipinkmenon1
 
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerStudy on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerAnamika Sarkar
 
GDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSCAESB
 
Biology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxBiology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxDeepakSakkari2
 
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...VICTOR MAESTRE RAMIREZ
 
main PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidmain PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidNikhilNagaraju
 

Recently uploaded (20)

Application of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptxApplication of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptx
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
 
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionSachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
 
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptxExploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
 
Current Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCLCurrent Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCL
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptx
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )
 
microprocessor 8085 and its interfacing
microprocessor 8085  and its interfacingmicroprocessor 8085  and its interfacing
microprocessor 8085 and its interfacing
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024
 
Introduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptxIntroduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptx
 
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerStudy on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
 
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
 
GDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentation
 
Biology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxBiology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptx
 
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
 
main PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidmain PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfid
 

Ch06Part1.ppt

  • 2. Copyright © 2006 Addison-Wesley. All rights reserved. 6-2 Chapter 6 Topics • Introduction • Primitive Data Types • Character String Types • User-Defined Ordinal Types • Array Types • Associative Arrays • Record Types • Union Types • Pointer and Reference Types
  • 3. Copyright © 2006 Addison-Wesley. All rights reserved. 6-3 Introduction • A data type defines a collection of data objects and a set of predefined operations on those objects • A descriptor is the collection of the attributes of a variable • An object represents an instance of a user-defined (abstract data) type • One design issue for all data types: What operations are defined and how are they specified?
  • 4. Copyright © 2006 Addison-Wesley. All rights reserved. 6-4 Primitive Data Types • Almost all programming languages provide a set of primitive data types • Primitive data types: Those not defined in terms of other data types • Some primitive data types are merely reflections of the hardware, e.g. integer types • Others require a little non-hardware support for their implementation
  • 5. Copyright © 2006 Addison-Wesley. All rights reserved. 6-5 Primitive Data Types: Integer • Almost always an exact reflection of the hardware so the mapping is trivial • Java’s signed integer sizes: byte, short, int, long • A signed integer value is represented by a string of bits with usually the leftmost bit representing the sign. Type Size(bits) Def. value Min. value Max. value Byte 8 0 -128 +127 Short 16 0 -32768 +32767 Int 32 0 -2147483648 +2147483647 Long 64 0 -9223372036854775808 +9223372036854775807
  • 6. Copyright © 2006 Addison-Wesley. All rights reserved. 6-6 Primitive Data Types: Floating Point • Model real numbers, but only as approximations, e.g. Pi • Languages for scientific use support at least two floating-point types, e.g., float(usually stored in 4 bytes of memory) and double(8 bytes of memory, provides larger fractional part); •
  • 7. Copyright © 2006 Addison-Wesley. All rights reserved. 6-7 Primitive Data Types: Decimal • For business applications (money) – Essential to COBOL – C# offers a decimal data type • Store a fixed number of decimal digits, with the decimal point at a fixed position in the value, unlike float or double data types which store approximation many times • Stored like character string, using binary codes for the decimal digits • More information about decimal data type: – http://msdn2.microsoft.com/enus/library/aa691147(VS.71).aspx – http://technet.microsoft.com/en-us/library/ms187912.aspx
  • 8. Copyright © 2006 Addison-Wesley. All rights reserved. 6-8 Primitive Data Types: Decimal • Advantage: accuracy • Disadvantages: – limited range (no exponents are allowed) – wastes memory, stored one digit per byte, sometimes two digits per byte.
  • 9. Copyright © 2006 Addison-Wesley. All rights reserved. 6-9 Primitive Data Types: Boolean • Simplest of all • Range of values: two elements, one for “true” and one for “false” • C89: all operands with nonzero values are considered true, and zero is considered false when used in conditionals. • C99 and C++ have a Boolean type, but they also allow numeric expressions to be used as if they were Boolean. (Not the case in Java and C#) • Often used to represent switches or flags • More readable than using integers
  • 10. Copyright © 2006 Addison-Wesley. All rights reserved. 6-10 Primitive Data Types: Character • Stored as numeric codings • Most commonly used coding: ASCII – Uses 0 to 127 to code 128 different characters • An alternative, 16-bit coding: Unicode – Includes characters from most natural languages – First 128 characters are identical to ASCII – Originally used in Java – C# and JavaScript also support Unicode
  • 11. Copyright © 2006 Addison-Wesley. All rights reserved. 6-11 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?
  • 12. Copyright © 2006 Addison-Wesley. All rights reserved. 6-12 Character String Type in Certain Languages • C and C++ – Not primitive – Use char arrays and a library of functions that provide operations – Character strings are terminated with a special character, null, which is represented with zero. – The library operations simply carry out their operations until the null character being operated on. Library functions that produce strings often supply the null character. – Common library functions: • strcpy, strcat, strcmp, strlen
  • 13. Copyright © 2006 Addison-Wesley. All rights reserved. 6-13 Character String Type in Certain Languages • C and C++ – String manipulation functions of the C standard library is unsafe, as they don’t guard against overflowing the destination. E.g.: strcpy(src, dest); If the length of dest is 20 and the length of src is 50, strcpy will write over the 30 bytes that follow dest, which is often in the run-time stack. - C++ programmer should use the string class from the standard library
  • 14. Copyright © 2006 Addison-Wesley. All rights reserved. 6-14 Character String Types Operations • Java: – Strings are supported as a primitive type by the String class [string a=“abcd” is the same as String a = new String(“abcd”)], whose values are constant strings (each time when the value is changed, a new String object is created). – StringBuffer class: values of a string is changeable. • C#: – Similar to Java • C++: – C-style strings and strings in its standard class library which is similar to that of Java
  • 15. Copyright © 2006 Addison-Wesley. All rights reserved. 6-15 Character String Types Operations • Pattern matching – Fundamental character string operation – Often called regular expressions – E.g., /[A-Za-z][A-Za-zd]+ matches string that begin with a letter, followed by one or more letters or digits – Perl, JavaScript and PHP have built-in pattern matching operations – Java, C++ and C# have pattern matching capabilities in the class libraries, e.g. Java: Pattern p = Pattern.compile("a*b"); Matcher m = p.matcher("aaaaab"); boolean b = m.matches();
  • 16. Copyright © 2006 Addison-Wesley. All rights reserved. 6-16 Character String Length Options • Static Length: COBOL, Java’s String class – Length is set when the string is created, fixed length • Limited Dynamic Length: C and C++ – Length is set to have a maximum, varying length • Dynamic Length(no maximum): Perl, JavaScript – No length limit, varying length. Required overhead of dynamic storage allocation and deallocation but provides maximum flexibility • Ada supports all three string length options: – String: static – Bounded_String: limited dynamic – Unbounded_String: dynamic
  • 17. Copyright © 2006 Addison-Wesley. All rights reserved. 6-17 Character String Type Evaluation • Aid to writability • As a primitive type with static length, they are inexpensive to provide • Simple pattern matching and catenation are essential, should be included • Dynamic length is most flexible, but overhead of implementation must be weighed. Often included only in languages that are interpreted.
  • 18. Copyright © 2006 Addison-Wesley. All rights reserved. 6-18 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++) type of string address of the first character type of string address of the first character
  • 19. Copyright © 2006 Addison-Wesley. All rights reserved. 6-19 Character String Implementation • Dynamic length: need run-time descriptor; allocation/de-allocation is the biggest implementation problem. Two approches: – String is stored in a linked list, so when a string grows, the newly required cells can come from anywhere in the heap. Drawback – extra storage occupied by the links in the list representation and the necessary complexity of string operations. But allocation and deallocation process is simple. – Store complete strings in adjacent storage cells. When the storage for the adjacent cell is not available(when the string grows), a new area of memory is found to store the complete new string. Faster string operation and requires less storage, but slower allocation and deallocation process. This approach is typically used.
  • 20. Copyright © 2006 Addison-Wesley. All rights reserved. 6-20 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
  • 21. Copyright © 2006 Addison-Wesley. All rights reserved. 6-21 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}; • The enumeration constants are typically implicitly assigned the integer values, 0, 1, …, but can be explicitly assigned any integer literal in the type’s definition • Design issues – Is an enumeration constant allowed to appear in more than one type definition, and if so, how is the type of an occurrence of that constant checked? – Are enumeration values coerced to integer? – Any other type coerced to an enumeration type?
  • 22. Copyright © 2006 Addison-Wesley. All rights reserved. 6-22 Design • In languages that do not have enumeration types, programmers usually simulate them with integer values. E.g. Fortran 77, use 0 to represent blue and 1 to represent red: INTEGER RED, BLUE DATA RED, BLUE/0,1/ Problem: there is no type checking when they are used. It would be legal to add two together. Or they can be assigned any integer value thus destroying the relationship with the colors.
  • 23. Copyright © 2006 Addison-Wesley. All rights reserved. 6-23 Design • In C++, we could have enum colors {red, blue, green, yellow, black}; colors myColor = blue, yourColor = red; The enumeration values are coerced to int when they are put in integer context. E.g. myColor++ would assign green to myColor. • In Java, all enumeration types are implicitly subclasses of the predefined class Enum. They can have instance data fields, constructors and methods.
  • 24. Copyright © 2006 Addison-Wesley. All rights reserved. 6-24 Design – Java Example Enumeration days; Vector dayNames = new Vector(); dayNames.add("Monday"); … dayNames.add("Friday"); days = dayNames.elements(); while (days.hasMoreElements()) System.out.println(days.nextElement()); • C# enumeration types are like those of C++ except that they are never coerced to integer. Operations are restricted to those that make sense. The range of values is restricted to that of the particular enumeration type.
  • 25. Copyright © 2006 Addison-Wesley. All rights reserved. 6-25 Evaluation of Enumerated Type • Aid to readability, e.g., no need to code a color as a number • Aid to reliability, e.g., compiler can check: – operations (don’t allow colors to be added) – No enumeration variable can be assigned a value outside its defined range, e.g. if the colors type has 10 enumeration constants and uses 0 .. 9 as its internal values, no number greater than 9 can be assigned to a colors type variable. – Ada, C#, and Java 5.0 provide better support for enumeration than C++ because enumeration type variables in these languages are not coerced into integer types
  • 26. Copyright © 2006 Addison-Wesley. All rights reserved. 6-26 Evaluation of Enumerated Type • C treats enumeration variables like integer variables; it does not provide either of the two advantages. • C++ is better. Numeric values can be assigned to enumeration type variables only if they are cast to the type of the assigned variable. Numeric values are checked to determine in they are in the range of the internal values. However if the user uses a wide range of explicitly assigned values, this checking is not effective. E.g. – enum colors {red = 1, blue = 100, green = 100000} A value assigned to a variable of colors type will only be checked to determine whether it is in the range of 1..100000. • Java 5.0, C# and Ada are better, as variables are never coerced to integer types
  • 27. Copyright © 2006 Addison-Wesley. All rights reserved. 6-27 Subrange Types • An ordered contiguous subsequence of an ordinal type • Not a new type, but a restricted existing type – Example: 12..18 is a subrange of integer type • Ada’s design type Days is (mon, tue, wed, thu, fri, sat, sun); subtype Weekdays is Days range mon..fri; subtype Index is Integer range 1..100; Day1: Days; Day2: Weekday; Day2 := Day1; //legal if Day1 it not sat or sun Compatible with its parent type.
  • 28. Copyright © 2006 Addison-Wesley. All rights reserved. 6-28 Subrange Evaluation • Aid to readability – Make it clear to the readers that variables of subrange can store only certain range of values • Reliability – Assigning a value to a subrange variable that is outside the specified range is detected as an error
  • 29. Copyright © 2006 Addison-Wesley. All rights reserved. 6-29 Implementation of User-Defined Ordinal Types • Enumeration types are implemented as integers • Subrange types are implemented like the parent types with code inserted (by the compiler) to restrict assignments to subrange variables
  • 30. Copyright © 2006 Addison-Wesley. All rights reserved. 6-30 Array Types • An array is an aggregate of homogeneous data elements in which an individual element is identified by its position in the aggregate, relative to the first element.
  • 31. Copyright © 2006 Addison-Wesley. All rights reserved. 6-31 Array Design Issues • What types are legal for subscripts? • Are subscripting expressions in element references range checked? • When are subscript ranges bound? • When does allocation take place? • Are ragged or rectangular multidimensioned arrays allowed, or both? • Can arrays be initialized when they have their storage allocated? • Are any kind of slices allowed?
  • 32. Copyright © 2006 Addison-Wesley. All rights reserved. 6-32 Array Indexing • Indexing (or subscripting) is a mapping from indices to elements array_name (index_value_list)  an element • Index Syntax – FORTRAN, PL/I, Ada use parentheses • Ada explicitly uses parentheses to show uniformity between array references and function calls because both are mappings – Most other languages use brackets
  • 33. Copyright © 2006 Addison-Wesley. All rights reserved. 6-33 Arrays Index (Subscript) Types • FORTRAN, C: integer only • Pascal: any ordinal type (integer, Boolean, char, enumeration) • Ada: integer or enumeration (includes Boolean and char) • Java: integer types only • C, C++, Perl, and Fortran do not specify range checking • Java, ML, C# specify range checking