I will also seek to address the advantages of selectively using different data types, and why different data types exist. This report will discuss seven data types I consider to be the most important for the creation of a memory-efficient program, and these fall under three categories: Number types Character types Logical type Number Types These variable types form the very foundation of a programming language. With these, we can calculate values and actually compute data. Depending on the types of number data being used, there are two kinds of number types we can use: whole / integer number and floating point / decimal number.
Whole numbers we use when we don’t require calculations to be too precise, such as scientific equations, so we need to make sure of this before we publish the program. Depending on how large we want our integers to go up to, or how efficient we want to be with our program’s RAM use, we can choose to use bytes, integers, or longs (otherwise called long integers). Bytes have a range of 256 units, whereas long integers have a range of 4,294,967,296 units. If we want to be able to use minus numbers, we also have to be aware that bytes will not be able to support these.
(Mark Fishpool, 2011) In this comparison, bytes are less useful, but also happen to be two to four times as efficient when it comes to the amount of reserved space it creates in memory. Below is a table of these three integer types with their ranges and size in bytes: Whole Number Types Data Type Storage Size Range / Value Byte 8 bits / 1 byte 0 to 255 Integer 16 bits / 2 bytes -32,768 to 32,767 Long 32 bits / 4 bytes -2,147,483,648 to 2,147,483,647 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: Decimal Number Tyfor positive values 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: Result of “40 + 8” Using Number Type Result of “40 + 8” Using Character Type 48 408 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: Character Types Data Type Storage Size Range / Value String (fixed) Length of string 1 to approximately 65,400 String (variable) 10 bytes + string length 0 to approximately 2 billion 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) Logical Type Data Type Storage Size Range / Value Boolean Theoretically 1 bit (32 bits / 4 bytes in VB . NET) TRUE or FALSE 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:
Single floating point number Truncated to integer 8. 9999999999 9 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: Dim Statement Resulting Data Type Dim myNumber String Dim myNumber As Int Integer
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.