Logout

D.1.9

Explain the need for different data types to represent data items.

 

Teaching Note:

The data types will be restricted to integer, real, string and Boolean.

 

Sample Question:

sdfsdfsf

JSR Notes:

 

The Assessment Statement "Need"

Then there's the whole idea of having different types to represent different data. It makes sense to have both integer and real number data types, since the maximum and minimum range of integers would be limited by the requirement to look after decimal places, and often decimal places are not necessary. Conversely, it is good to have a data type which can indeed keep track of many decimal places, in order to more accurately represent real number values. And individual characters - on their own, or together as Strings - obviously need to be able to be represented also. Then there's the ever-present boolean situation in programming, and so it makes sense to have a data type specialized to represent true or false only.

The need for each mentioned in the assessment statement teaching note:

integer - to represent whole number values, which are used frequently in mathematics. Furthermore, ints are used very frequently for counting type procedures, such as keeping track of how many times a loop should execute, and for naming elements in arrays.

real - it is important to be able to represent real numbers, since they are used - indeed are expeced to be used - in much mathematics, and real world measurement, and it is often very important to be able to accurately represent precise values to many decimal places

string - we need to be able to represent words, since words are our main means of human communication. It makes sense to have a data structure which can handle words, i.e. groups of characters, or indeed "strings" of characters; this is easier than dealing directly with arrays of characters.

Boolean - computers are really good at two basic things: repetition (indeed very, very quick repetition), and working with structures that are able to act/react differently depending on certain conditions. Having a boolean data type allows these structures to operate differntly depending on such variable conditions.

 

 

The Other Reason - Memory Management (Not part of the assessment statement, but I think important to know.)

From a memory standpoint, its that you don't want to waste memory. A boolean, in theory, could be represented with just one bit, though in Java it uses 8. An integer does not need as many bits to represent integer values (for example, the Java 32-bit int, representing from -2 billion to +2 billion) as a real number with a fair precision (for example, the 64-bit Java double). And Strings will be as much memory as the number of characters they have (using 16-bit individual Java chars).

So with the memory management view, particularly with the three integer Java data types (short, int and long), and two real number Java data types (float, and double), you want to match your desired integer range/real number precision needs with the appropriate type. Since short can only deal with -32,768 <---> 32,767, you could not use it to work with the population of countries, but if it were to keep track of numbers of students enrolled in various high schools, then it would make sense. And for any real number value for which you need high precision, you need to use double, but if it's just for general outside temperature values, float would be good enough, and not unduly waste memory.

Here's a board diagram which shows how using the int data type for small numbers (in this case, all under 100) wastes memory; the short data type could more efficiently be used:

int memory wastage