148 Using Different Data Types Using Different Data Types Defining a specific data type makes it easy to ✦ Know the type of data a variable can hold (either numbers or text). ✦ Restrict the range of data the variable can hold. ✦ Identify whether a variable can hold a number or text can keep your program from trying to add or multiply a number by a word, which doesn’t work. Figure 2-3 shows the different categories of data types within most programming languages. Common Programming Data Types Data Types Figure 2-3: Different data types that can hold a range of values. Boolean (0 or 1) Integer (Whole numbers) Byte (0–255) Integer (-32,768– 32,767) Long (-2.147e9– 2.127e9) Numbers Real (Decimal numbers) Single (-3.403e38– 2.147e9) Double (-1.798e308– 1.798e308 Character (1 character) Text String (0 to 2 billion characters Defining the range of data can prevent a variable from storing incorrect data. For example, if your program asks the user to type in his age, invalid data would include negative numbers, zero, and extremely large numbers, such as 259. The range of numbers listed for Long, Single, and Double data types are listed as exponential numbers (that’s the little e). So the Long data type can store a number as large as 2.147 with the decimal place moved nine places to the right or approximately 2,147,000,000. Every programming language offers different data types, so use Figure 2-3 as a guideline rather than a strict reference. For example, if you want to store a person’s age in a variable, you probably want to store it as a whole number (such as 45) rather than a real number (such as 45.029). Next, you want to choose a data type that contains the range of values a person’s age might be. From Figure 2-3, you can see that a Boolean data
Using Different Data Types 149 type can only hold a 0 or 1. An Integer data type can hold negative numbers as small as -32,768 or positive numbers as large as 32,767. However, the range of an Integer data type is too large for a person’s age. Nobody has a negative age and nobody has an age anywhere close to 32,767. So the best data type to choose for storing a person’s age is the Byte data type, which can store numbers from 0 to 255. If your program tries to store a number less than 0 or greater than 255 as a person’s age, the Byte data type screams in protest and refuses to do it, a process known as type-checking. Type-checking only makes sure that your program doesn’t try to store incorrect data in a variable, but it can’t protect against a clumsy user typing in his age as a negative number or a massively unrealistic number. To prevent user input error, programs must validate that the data is correct. If a person types 0 for his age, your program must refuse to accept the incorrect data and ask the user to try again. That means you must always write extra commands in your program to check for valid data that’s received from the outside world. Another reason to use different data types is to reduce the amount of memory your program needs. It’s possible to declare an Age variable as either a Byte or Integer data type, as shown in the following Visual Basic code: Book II Chapter 2 Variables, Data Types, and Constants Dim Age as Byte Or Dim Age as Integer Although both Age variables can store whole numbers, such as 39, the Byte data type uses less memory (1 byte) than the Integer data type (4 bytes). A byte is just a measurement unit where 1 byte represents the space needed to store a single character (such as B or 8). In comparison, a typical sentence requires 100 bytes of storage, a page in a book might require 10,000 bytes, and a novel might require 1 million bytes. Bytes measure both storage space and memory so a Byte data type (storing the number 48) needs only 1 byte of space whereas an Integer data type (storing the same number 48) would need four times as much space. If you choose the wrong data type, your program still works, but it uses up more hard disk space or memory than necessary. Table 2-1 lists common storage requirements for different data types.
Agreed in 2016, the motive of the General Data Protection Regulation (GDPR) is to better protect the personal data of European Union “data subjects” – EU citizens and other nationals physically present in the EU at the time data are collected. Visit: https://www.hipaajournal.com/gdpr-training/