Below is a table of these three integer types with their ranges and size in bytes:
Floating point / decimal integers, although requiring more reserved space in RAM than normal integers, can be more precise. They are called floating points because the decimal is not fixed, and can be moved around. (Wikipedia, 2011) Depending on how precise our numbers are required to be and how many decimal points we need to go to, we can choose either a single precision type or double precision type, abbreviated to single and double respectively. A double point variable of course requires double the memory capacity as a single.
Below is a table of these two floating point types with their ranges and size in bytes:
Character Types
While number types are our bread and butter of data, we also require the use of characters to make our programs interactive for those of us who don’t speak in binary. A computer is able to display human readable alphabetic characters by being able to refer to each one as a series of 8 numbers, or bits. In order to tell a computer to show the letter L, at its most basic level I would tell it to refer to 01001100 on its ASCII table. (Kozierok, 2004) Because of this, each alphabetic letter takes up 8 bits (or more commonly known as 1 byte) in computer memory.
Because of this, alphabetic character types are handled differently compared to number types. The contents of strings cannot be manipulated in the same way that number types can, including numbers. The following table demonstrates an example of how this works. While the calculation is carried out in the number type, the two character strings with alphanumeric digits in them are simply shoved together:
When we are using strings, we also need to be aware of how many characters we may require to be in our variable. A normal fixed string variable can hold 65,400 units, while a variable string can hold up to about 2,000,000,000 units. This flexibility comes at the cost of 10 additional bytes being reserved.
Below is a table of these two character types with their ranges and size in bytes:
Logical Type
Otherwise known as a Boolean, this is a special type of variable which can store only one value – True or false. These therefore make perfect additions to a program which utilise If statements, loops, or other scenarios where comparisons between data are made. The size of the Boolean in memory depends on the language being used. In .NET, it is 4 bytes (32 bits) (Wikibooks, 2011), while in Java it is 1 bit (1/8th of a byte) (Mark Fishpool, 2011). While a Boolean at its most basic is only 1 bit (true or false; 0 or 1), .NET adds provides this extra padding to make it more efficient for a 32bit CPU to read, by sending it as 32bits of data. (Stack Overflow, 2008)
Benefits of Different Data Types
There are a number of reasons why choosing the most appropriate data type for a variable is important. Aside from improved efficiency in memory use and speed of execution, using incorrect data types can jeopardise the integrity of a program’s data.
Accurate Data
For number types, the most important reason for using an appropriate data type is to make sure the integrity of the data is maintained. If a whole or decimal type is used with too few floating points, the meaning the value it can store is too small, it will truncate (or shorten) the value to the nearest value it can, and therefore making the data invalid. (Mark Fishpool, 2011) The following table contains an example of this:
An integer data type cannot contain decimals, so it rounds it up to the nearest number it can contain, which happens to be 9.
Efficient Memory Usage
When a program application is opened, the storage size of each value is allocated and reserved in memory. Regardless of the value of the data in each variable, with the exception of strings, each data type has a set size in bytes. This means that a double will still use up 8 bytes, even if only a single digit value is stored inside it – The computer is not able to dynamically change this.
In a simple beginner’s program, the importance of saving several tens of bytes in memory will not seem clear and may be viewed as unnecessary. However, when the scale of a piece of software or a database becomes industrial size with hundreds of users connected at once, this wastage can become crippling.
Data Validation
This refers to making sure that data is “clean, correct and useful” (Wikipedia 2, 2011). One of the consequences of using incorrect data types is causing the program to crash. This can happen, for instance, as a result of using alphabetic data in a numeric variable. The program doesn’t know what to do with it, and the crash is the result of a logic failure.
One way of preventing this from happening and increasing stability is to set the default type of all new variables as one that is flexible, all encompassing, and will prevent accidental crashes like these from happening. In Visual Basic, this is achieved by setting the default data type of new variables as string. The below table demonstrates this:
The programmer is of course free to change the data type as they wish, but with prudence, a variable is only set as a number or logic type only when the contents have been validated. (Mark Fishpool, 2011)
Summery
In this report, we had a look at what data types are available and on what basis they are used. We learned about the varying storage sizes available for different kinds of numerical data and the resulting change in byte storage size.
We also had a look at the advantages of selectively using different data types, and why different data types exist. We saw that using data types correctly made sure that the integrity of numerical data is preserved, that choosing memory-efficient data types is prudent, and that maintaining a data type consistency for new variables increases the stability of the program.
Bibliography
Kozierok, C. (2004). ASCII Character Chart. Retrieved October 10, 2011, from The PC Guide: http://www.pcguide.com/res/tablesASCII-c.html
Mark Fishpool, B. F. (2011). BTEC Level 3 National in IT, Second Edition. Hodder Education.
Stack Overflow. (2008). Why in .NET System.Boolean takes 4 byte? Retrieved October 10, 2011, from Stack Overflow: http://stackoverflow.com/questions/294905/why-in-net-system-boolean-takes-4-byte
Wikibooks. (2011). Visual Basic .NET/Variables. Retrieved October 10, 2011, from Wikibooks: http://en.wikibooks.org/wiki/Visual_Basic_.NET/Variables
Wikipedia 2. (2011). Data Validation. Retrieved October 10, 2011, from Wikipedia: http://en.wikipedia.org/wiki/Data_validation
Wikipedia. (2011). Floating Point. Retrieved October 10, 2011, from Wikipedia: http://en.wikipedia.org/wiki/Floating-point