Complex Data Types
• Some languages support a complex type,
e.g., C99, Fortran, and Python
• Each value consists of two floats, the real
part and the imaginary part
• Literal form (in Python):
(7 + 3j), where 7 is the real part and 3 is
the imaginary part
Copyright © 2009 Addison-Wesley. All rights reserved. 6-17
Primitive Data Types: Decimal
• For business applications
o Essential to COBOL
oC# offers a decimal data type
• Store a fixed number of decimal digits, in
coded form (BCD) - One byte per digit
• Advantage: accuracy
• Disadvantages: limited range, wastes
memory
Copyright © 2009 Addison-Wesley. All rights reserved. 6-18
C# decimal type
• 128 bits = 16 byte representation
• Range: 1.0X10-28 to 7.9x1028
• Precision: exact to 28 or 29 decimal
places
Copyright © 2009 Addison-Wesley. All rights reserved. 6-19
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 is more
often implemented as bytes
o Advantage: readability
Copyright © 2009 Addison-Wesley. All rights reserved. 6-20
Primitive Data Types: Character
• Stored as numeric coding
• Most commonly used coding: ASCII
• An alternative,16-bit coding: Unicode (UCS-2)
o Includes characters from most natural languages
o Originally used in Java
o C# and JavaScript also support Unicode
• 32-bit Unicode (UCS-4)
o Supported by Fortran, starting with 2003
Copyright © 2009 Addison-Wesley. All rights reserved. 6-21
http://www.asciitable.com/
Character String Types
• Values are sequences of characters
• Design issues:
o Is it a primitive type or just a special kind of
array?
o Should the length of strings be static or
dynamic (determined at runtime)?
oWhat are the kinds of operations that you
would perform on a string?
Copyright © 2009 Addison-Wesley. All rights reserved. 6-22
Character String Types Operations
• List the typical operations on Strings:
o Assignment and copying
oComparison (=, >, etc.)
oCatenation
o Substring reference
o Pattern matching
Copyright © 2009 Addison-Wesley. All rights reserved. 6-23
String Type in Certain Languages
• C and C++ o Not primitive
o Use char arrays and a library of functions that provide operations
• SNOBOL4 (a string manipulation language) o Primitive
o Many operations, including elaborate pattern matching
• Fortran and Python (http://docs.python.org/library/string.html) o Primitive type with assignment and several operations
• Java o Primitive via the String class
• Perl, JavaScript, Ruby, and PHP o Provide built-in pattern matching, using regular expressions
Copyright © 2009 Addison-Wesley. All rights reserved. 6-24
Character String Length Options
• Static: COBOL, Java’s String class
• Limited Dynamic Length: C and C++
o In these languages, a special character is
used to indicate the end of a string’s
characters, rather than maintaining the length
• Dynamic (no maximum): SNOBOL4, Perl,
JavaScript
• Ada supports all three string length options
o Static, Limited Dynamic and Dynamic
Copyright © 2009 Addison-Wesley. All rights reserved. 6-25
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?
Copyright © 2009 Addison-Wesley. All rights reserved. 6-26
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
Copyright © 2009 Addison-Wesley. All rights reserved. 6-27
Compile- and Run-Time Descriptors
Copyright © 2009 Addison-Wesley. All rights reserved. 6-28
Compile-time descriptor for static strings
Run-time descriptor for limited dynamic strings
3-mins to discuss your string length type
Static, Limited Dynamic, Dynamic
• Static length strings are always full: if a
shorter string is assigned to a string variable,
the empty characters are set to blanks.
o Efficient but not very flexible and wasteful.
• Limited dynamic length - can store any
number of characters between zero and
the maximum, fast but not very flexible and
wasteful
• Dynamic length - slower but very flexible
and less wasteful
Copyright © 2009 Addison-Wesley. All rights reserved. 1-29
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
o integer
o char
o boolean
Copyright © 2009 Addison-Wesley. All rights reserved. 6-30