Data Types
KENNETH LEROY BUSBEE AND DAVE BRAUNSCHWEIG
Overview
A data type is a classification of data which tells the compiler or interpreter how the programmer
intends to use the data. Most programming languages support various types of data, including
1
integer, real, character or string, and Boolean.
Discussion
Our interactions (inputs and outputs) with a program are treated in many languages as a stream
of bytes. These bytes represent data that can be interpreted as representing values that we
understand. Additionally, within a program, we process this data in various ways such as adding
them up or sorting them. This data comes in different forms. Examples include:
• your name – a string of characters
• your age – usually an integer
• the amount of money in your pocket – usually a value measured in dollars and cents
(something with a fractional part)
A major part of understanding how to design and code programs is centered in understanding the
types of data that we want to manipulate and how to manipulate that data.
Common data types include:
Data Type Represents Examples
integer whole numbers -5 , 0 , 123
floating point (real) fractional numbers -87.5 , 0.0 , 3.14159
string A sequence of characters "Hello world!"
Boolean logical true or false true , false
nothing no data null
The common data types usually exist in most programming languages and act or behave similarly
from language to language. Additional complex and/or composite data types may exist and vary
from language to language.
1. Wikipedia: Data type
68 | Data Types
Pseudocode
Function Main
... This program demonstrates variables, literal constants, and data typ
Declare Integer i
Declare Real r
Declare String s
Declare Boolean b
Assign i = 1234567890
Assign r = 1.23456789012345
Assign s = "string"
Assign b = true
Output "Integer i = " & i
Output "Real r = " & r
Output "String s = " & s
Output "Boolean b = " & b
End
Output
Integer i = 1234567890
Real r = 1.23456789012345
String s = string
Boolean b = true
Data Types | 69
Flowchart
70 | Data Types
Data Types | 71
Key Terms
Boolean
A data type representing logical true or false.
data type
Defines a set of values and a set of operations that can be applied on those values.
floating point
A data type representing numbers with fractional parts.
integer
A data type representing whole numbers.
string
A data type representing a sequence of characters.
References
• cnx.org: Programming Fundamentals – A Modular Structured Approach using C++
• Flowgorithm – Flowchart Programming Language
72 | Data Types
Integer Data Type
KENNETH LEROY BUSBEE AND DAVE BRAUNSCHWEIG
Overview
An integer data type represents some range of mathematical integers. Integral data types may be
of different sizes and may or may not be allowed to contain negative values. Integers are commonly
represented in a computer as a group of binary digits (bits). The size of the grouping varies so the set
of integer sizes available varies between different types of computers and different programming
1
languages.
Discussion
The integer data type basically represents whole numbers (no fractional parts). The integer values
jump from one value to another. There is nothing between 6 and 7. It could be asked why not make
all your numbers floating point which allow for fractional parts. The reason is threefold. First, some
things in the real world are not fractional. A dog, even with only 3 legs, is still one (1) dog not ¾ of a
dog. Second, the integer data type is often used to control program flow by counting, thus the need
for a data type that jumps from one value to another. Third, integer processing is significantly faster
within the CPU than is floating point processing.
The integer data type has similar attributes and acts or behaves similarly in all programming
languages that support it.
1. Wikipedia: Integer (computer science)
Integer Data Type | 73
Language Reserved Word Size Range
C++ short 16 bits / 2 bytes -32,768 to32,767
C++ int varies depends on compiler
C++ long 32 bits / 4 bytes -2,147,483,648 to 2, 147,483,647
C++ long long 64 bits / 8 bytes −9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
C# short 16 bits / 2 bytes -32,768 to32,767
C# int 32 bits / 4 bytes -2,147,483,648 to 2, 147,483,647
C# long 64 bits / 8 bytes −9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
Java short 16 bits / 2 bytes -32,768 to32,767
Java int 32 bits / 4 bytes -2,147,483,648 to 2, 147,483,647
Java long 64 bits / 8 bytes −9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
JavaScript N/A
Python int() no limit
Swift Int varies depends on platform
Swift Int32 32 bits / 4 bytes -2,147,483,648 to 2, 147,483,647
Swift Int64 64 bits / 8 bytes −9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
For C++ and Swift the size of a default integer varies with the compiler being used and the
computer. This effect is known as being machine dependent. These variations of the integer data
type are an annoyance for a beginning programmer. For a beginning programmer, it is more
important to understand the general attributes of the integer data type that apply to most
programming languages.
JavaScript does not support an integer data type, but the Math.round() function may be
2
used to return the value of a number rounded to the nearest integer.
Python 3 integers are not limited in size, however, sys.maxsize may be used to determine
3
the maximum practical size of a list or string index.
Key Terms
machine dependent
An attribute of a programming language that changes depending on the computer’s CPU.
2. Mozilla: Math.round()
3. Python.org: Integers
74 | Integer Data Type
References
• cnx.org: Programming Fundamentals – A Modular Structured Approach using C++
Integer Data Type | 75
Floating-Point Data Type
KENNETH LEROY BUSBEE AND DAVE BRAUNSCHWEIG
Overview
A floating-point data type uses a formulaic representation of real numbers as an approximation so
as to support a trade-off between range and precision. For this reason, floating-point computation
is often found in systems which include very small and very large real numbers, which require
fast processing times. A number is, in general, represented approximately to a fixed number of
1
significant digits and scaled using an exponent in some fixed base.
Discussion
The floating-point data type is a family of data types that act alike and differ only in the size of their
domains (the allowable values). The floating-point family of data types represents number values
with fractional parts. They are technically stored as two integer values: a mantissa and an exponent.
The floating-point family has the same attributes and acts or behaves similarly in all programming
languages. They can always store negative or positive values thus they always are signed; unlike the
integer data type that could be unsigned. The domain for floating-point data types varies because
they could represent very large numbers or very small numbers. Rather than talk about the actual
values, we mention the precision. The more bytes of storage the larger the mantissa and exponent,
thus more precision.
Language Reserved Word Size Precision Range
C++ float 32 bits / 4 bytes 7 decimal digits ±3.40282347E+38
C++ double 64 bits / 8 bytes 15 decimal digits ±1.79769313486231570E+308
C# float 32 bits / 4 bytes 7 decimal digits ±3.40282347E+38
C# double 32 bits / 4 bytes 15 decimal digits ±1.79769313486231570E+308
Java float 32 bits / 4 bytes 7 decimal digits ±3.40282347E+38
Java double 32 bits / 4 bytes 15 decimal digits ±1.79769313486231570E+308
JavaScript Number 64 bits / 8 bytes 15 decimal digits ±1.79769313486231570E+308
Python float() 64 bits / 8 bytes 15 decimal digits ±1.79769313486231570E+308
Swift Float 32 bits / 4 bytes 7 decimal digits ±3.40282347E+38
Swift Double 64 bits / 8 bytes 15 decimal digits ±1.79769313486231570E+308
1. Wikipedia: Floating-point arithmetic
76 | Floating-Point Data Type
Key Terms
double
The most often used floating-point family data type used.
mantissa exponent
The two integer parts of a floating-point value.
precision
The effect on the domain of floating-point values given a larger or smaller storage area in
bytes.
References
• cnx.org: Programming Fundamentals – A Modular Structured Approach using C++
Floating-Point Data Type | 77
String Data Type
KENNETH LEROY BUSBEE AND DAVE BRAUNSCHWEIG
Overview
A string data type is traditionally a sequence of characters, either as a literal constant or as some
kind of variable. The latter may allow its elements to be mutated and the length changed, or it may
be fixed (after creation). A string is generally considered a data type and is often implemented as
an array data structure of bytes (or words) that stores a sequence of elements, typically characters,
1
using some character encoding.
Discussion
Depending on programming language and precise data type used, a variable declared to be a
string may either cause storage in memory to be statically allocated for a predetermined maximum
length or employ dynamic allocation to allow it to hold a variable number of elements. When a
2
string appears literally in source code, it is known as a string literal or an anonymous string.
The character data type represents individual or single characters. Characters comprise a variety
of symbols such as the alphabet (both upper and lower case) the numeral digits (0 to 9),
punctuation, etc. All computers store character data in a one-byte field as an integer value. Because
a byte consists of 8 bits, this one-byte field has 28 or 256 possibilities using the positive values of 0
to 255.
C++, C#, and Java differentiate between single characters and strings using single quotes and
double quotes, respectively. JavaScript, Python, and Swift do not differentiate between characters
and strings and use either single quotes or double quotes to define string literals.
1. Wikipedia: String (computer science)
2. Wikipedia: String (computer science)
78 | String Data Type
Language Reserved Word Example
C++ char 'A'
C++ string "Hello world!"
C# char 'A'
C# String "Hello world!"
Java char 'A'
Java String "Hello world!"
JavaScript String 'Hello world!' , "Hello world!"
Python str() 'Hello world!' , "Hello world!"
Swift Character "A"
Swift String "Hello world!"
Most computing devices use the ASCII (stands for American Standard Code for Information
Interchange and is pronounced “ask-key”) Character Set which has established values for 0 to 127.
For the values of 128 to 255 they usually use the Extended ASCII Character Set. When we hit the
capital A on the keyboard, the keyboard sends a byte with the bit pattern equal to an integer 65.
When the byte is sent from the memory to the monitor, the monitor converts the integer value of
65 to into the symbol of the capital A to display on the monitor.
For now, we will address only the use of strings and characters as constants. Most modern
compilers that are part of an Integrated Development Environment (IDE) will color the source code
to help the programmer see different features more readily. Beginning programmers will use string
constants to send messages to standard output.
Key Terms
ASCII
American Standard Code for Information Interchange
character
A data type representing single text characters like the alphabet, numeral digits, punctuation,
etc.
double quote marks
Used to create string type data within most programming languages.
single quote marks
Used to create character type data within languages that differentiate between string and
character data types.
string
A series or array of characters as a single piece of data.
References
• cnx.org: Programming Fundamentals – A Modular Structured Approach using C++
String Data Type | 79
Boolean Data Type
KENNETH LEROY BUSBEE AND DAVE BRAUNSCHWEIG
Overview
A Boolean data type has one of two possible values (usually denoted true and false), intended
to represent the two truth values of logic and Boolean algebra. It is named after George Boole,
who first defined an algebraic system of logic in the mid 19th century. The Boolean data type is
primarily associated with conditional statements, which allow different actions by changing control
1
flow depending on whether a programmer-specified Boolean condition evaluates to true or false.
Discussion
The Boolean data type is also known as the logical data type and represents the concepts of
true and false. The name “Boolean” comes from the mathematician George Boole; who in 1854
published: An Investigation of the Laws of Thought. Boolean algebra is the area of mathematics
that deals with the logical representation of true and false using the numbers 0 and 1. The
importance of the Boolean data type within programming is that it is used to control programming
structures (if then else, while loops, etc.) that allow us to implement “choice” into our algorithms.
The Boolean data type has the same attributes and acts or behaves similarly in all programming
languages. However, while all languages recognize false as 0, some languages define true as -1
rather than 1. This is the result of storing the Boolean values as an integer and using a one’s
complement representation that negates all bits rather than only the rightmost bit. To simplify
processing, most programming languages recognize any non-zero value as being true.
Language Reserved Word True False
C++ bool true false
C# bool or Boolean true false
Java bool true false
JavaScript Boolean() true false
Python bool() True False
Swift Bool true false
1. Wikipedia: Boolean data type
80 | Boolean Data Type
Key Terms
Boolean
A data type representing the concepts of true or false.
one’s complement
The value obtained by inverting all the bits in the binary representation of a number
(swapping 0s for 1s and vice versa).
References
• cnx.org: Programming Fundamentals – A Modular Structured Approach using C++
Boolean Data Type | 81
Nothing Data Type
DAVE BRAUNSCHWEIG
Overview
A nothing data type is a feature of some programming languages which allow the setting of a
1
special value to indicate a missing or uninitialized value rather than using the value 0 (zero).
Discussion
Most programming languages support the use of a reserved word or words to represent missing,
uninitialized, or invalid values.
Language Reserved Word Meaning
C++ null no value
C# null no value
Java null no value
JavaScript null no value
JavaScript NaN Not a Number
Python None no value
Swift nil no value
Key Terms
NaN
Reserved word used to indicate a non-numeric value in a numeric variable.
null
Reserved word used to represent a missing value or invalid value.
1. Wikipedia: Nullable type
82 | Nothing Data Type