W5-Module-Handling Primitive Data Types
W5-Module-Handling Primitive Data Types
So what are we waiting for? Let us continue our exploration of the world of
Computer Programming.
Course Module
Recall
Introduction
Handling Variables
Variables
Explicit variables are variables that we declare, usually at the start of our
application before any processing is done within. They are made is such a
way that developers know they will be using a variable within the context of
the application. Most programming languages have explicit variables; they
require that variables are to be declared before they are used.
Implicit variables are variables that are used right away without the need of
declaring them. They are referenced using a name. They are made in such a
way that they are only allocated some memory when called. They are usable
within the context where they are initially used. Most programming
languages used in Web Development have implicit variables.
Anonymous variables are variables that are used right away without the need
of declaring them. They, however, are not referenced using a name. Only the
computer itself know how to call them. They are typically declared and
destroyed after use. Anonymous variables are used during computations
Major declarations are done as the first line of the “public void main()” function
for the explicit variables. Throughout the course, you will be using anonymous
variables without initially knowing you are using them.
We use variables in par with data types. Data Types are the restriction we
place to variables that limit them to handle only our specified type of data.
Think of this like we are segregating our valuables to label boxes. Box 1,
labeled as “Tommy’s Box”, will only contain the valuables of Tommy. Box 2,
labeled as “Furniture”, will only contain the furniture, and nothing else like
food. Variables behaves in the same way like labeled boxes, they are limited
to their declared data type. We have two (2) classifications for data types:
a) By Value
b) By Construct
Classification by Value
When data types are classified by value, we have explicit and implicit data
types. ExplicitData Typesare the data types that are declared and defined. For
example, we declared a string variable, therefore we can only store string
values to that variable. We declared an integer variable, therefore we can
only store integer values to that variable.
Explicit Data Types are often called “Strong-typed” in some other contexts. This
is mainly because of how the values to be stored are treated. In addition, being
called “Strong-typed” suggests that there exists a bound between the data
types, by extension including the variables, and the value stored.
Implicit Data Types are data types that are taken depending on how they are
stored or used. They are not defined which often causes ambiguity on how
the values are to be handles.
Implicit Data Types are often called “Variant” type. They are especially used in
Web Development wherein the values, like those which come from the users,
are initially treated like strings before they are converted to what they are
actually.
Course Module
Classification by Construct
Primitive Data Types are data types of singularity by nature. They contain
only the values they are supposed to contain. They do not have any other
components like properties and internal functions. They are like folders for
paper. They are used only to contain papers and nothing else like writing
pens, chips and cars.
Referenced Data Types are data types that are objective by nature. They do
not only contain the values they are supposed to contain, but also have other
components like properties and functions. They contain variables within
them. They are like car garage. They can contain anything within them, not
only the cars which they are made for. They can contain fuel containers,
workbenches and spare tires. In addition, they also serve different
functionalities like they enable automatic closing of garage doors, internal
lightings, and vents.
Primitive Data Types in C++ are classifiable in integers, floating points and
characters. This classification is based on what specific values are allowable.
Integers, floating points and booleans are numerical values, whereas
characters are textual values.
Integers
Integers are values that are numerical in nature. They do not have decimal
precisions. Some examples of these are counting numbers including zero
(0).Under Integers, we have the regularinteger, long and short data types.
Table 1 and 2 show the minimum and the maximum values allowable for the
Integer Values, both signed and unsigned integer values.
In addition to these data types, we have a singed and unsigned identifiers for
integer data types. When we do not specify any of the two identifiers, C++
implicitly interprets them as if you have placed a singed identifier.
Exceeding the set boundaries for the Integer Values results to what we call
“overflow”. Causing overflows results to unexpected outcomes which typically
results for the application to terminate.
Floating-points
Floating Points are values that are numerical in nature. They also sport
decimal precisions. Some examples are temperature, body-mass index and
height. Table 3 shows the minimum and maximum values allowable for
floating-point values.
Course Module
Data Type Identifier Minimum Value Maximum
Booleans
Boolean Values are values classifiable to either a true or false. These are
numerical in nature because C++ stores them as zero (0) or zero (1),
internally. True value for booleans are considered as non-zero positive
integer. False value for booleans are considered as zero. They are usually
used as flags for a specific scenario. They use bool identifier.
Characters
Characters are the only primitive data type that is textual in nature. Regular
characters can only represent one (1) character per variable. Strings can be
represented as a series of characters. That is why, in programming languages
such as C++, a string is declared as an array of characters. Characters use
char identifier, whereas Strings use char variable[ ]syntax.
Now that we have covered all the supported Primitive Data Types in C++, we
need to discern the importance of having different data types and when we
will generally use them in real-world applications.
Data Structures
First thing we need to know is how the computer in general, the Operating
System and the CPU in particular handle variables. When we declare a
variable, the computer allocates a specific empty and unused region in the
RAM where to store the supposed value. How big the allocation will be
depends on the maximum value of the data type. This is the main reason why
we have varying lengths for our integer and floating points.
The same thing applies for [signed] short integers. However, the least
significant bit (the left-most bit), symbolizes the parity of the sign. For the
value 32,767, we will have 0111 1111 1111 1111 in binary. For the value -
32,768, we will have 0111 1111 1111 1111 in binary.
A Character data type occupy 8-bits. The value differs on how the compiler
sets the value of the characters. For example, Compiler A can store the
character “A” as 0000 0000, while Compiler B can store the character “A” as
0100 0000.
The course about “Data Structures” will discuss in detail about binaries,
parities and the like which are relevant on how data are stored within
computers. This includes the complex operations the computer perform on
floating-points.
Real-world Application
Now that we understand how Data Types work technically and theoretically,
we need to discern what the most applicable data types per scenar io is. Since
there are an unlimited number of possible scenarios, we will only tackle the
most common and general of them.
Course Module
First is when we handle any numeric values, we know to choose only
between integers and floating-points. What determines which of the two will
be used relies on the need to store the decimal values. When we count the
number of occurrences of a specific object, we can use integer values. They
mainly differ on how large the value could go. For example we need to count
the number of cars in the garage, we can use short. If we take the overall
populations in the world, we need to use long.
One of the Internet giants, Google, recently changed the data type of their
“Thumbs Up” variable from “int” to due to a short commotion that occurred in
the official Psy’s Gangnam Style video in YouTube. This is because the limit of
int was reached forcing YouTube to change the data type to Int64 – essentially
the long data type in C++.
For values needing decimal precision, we use floating points. If we are simply
storing the temperature of a given specimen, we can use float. However, if we
take monetary values, we need to use doubles. This is mainly because
monetary values typically reach millions and billions.
If we only need to check the parity of an object or state, we only need to use
the Boolean. If we need to know whether the user is, biologically speaking, a
male. We can use “bool isMale;” rather than any other data types like the
storage heavy string. This is because if the “isMale” results to a false, we
automatically know the user is a female. There is no gender in between of the
two biologically speaking.
Course Module
References and Supplementary Materials
Books and Journals
Nell Dale; 2013; C++ Plus Data Structures, Fifth Edition; Massachusetts; Jones
& Bartlett Learning, LLC
Mike Dawson; 2015; Beginning C++ Through Game Programming, 4 th Edition;
Boston; Cengage Learning