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:

----

With reference to two examples from the classes on page xyz, explain the benefits
gained by the use of different data types.

----


sdfsdfsf



JSR Notes:

Data Types Review

Because data can come in many forms and sizes there is a need for various data types to suit the particular needs or situation. First, let's review what you should already know about data types:

What a Type Defines

Kinds of Types

What Types are at a Physical Level

Variables vs. Constants

When certain types are declared, recall also that they can be declared as wither variables, or constants. A constant, once declared, cannot have its value changed. In Java, it is the final keyword which dictates that the data's value is to be constant.

final int NUMBATHROOMS = 6; //from this point on, the value of x is 45 and cannot be chanaged.
NUMBATHROOMS = 7; // This line would cause an ERROR.
It is not possible to change NUMBATHROOMS.

Recall that it is a convention to make constant identifiers ALL CAPS, but that, in and of itself does not make it a constant; it is the use of the reserved word final, which does that.

Meantime, a variable is another kind of named memory location where a value is stored, but that value can be changed throughout the running of the program. For example:

int i = 52;
System.out.println(i); // 52 is printed out
i++;
System.out.println(i); // 53 is printed out


_____________________________________________________

 

The Need for Types

Reason for Types # 1. Different Kinds of Data

It makes a lot of sense to have ifferent types to represent different kinds of data. Starting with numbers, it makes sense to have both integer and real number data types. If we had only one general number type, 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. Meantime, 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.

Here is more on the need to have each type 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.

User Defined Types - Not all programming languages allow the creation of user defined types, but Java and many others do. This allows programmers to develop types which do more than the above primitive types can. User defined types are what we make in Java when we make Temlate classes. So a Car object, or a PimpedUpOrange object are able to do things and store things that primitives alone cannot.

 

Reason for Types # 2. Memory Management

The other general need for different types is memory management. From a memory standpoint, you don't want to waste memory; it's as simple as that. 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

 

In Summary, Back to the Assessment Statement:
"Explain the need for different data types to represent data items."

Having different data types allow different operations to be carried out depending upon the type of data being worked with.

So, for example, calculations can be performed using integer types, whereas numbers treated as Strings would not be able to be worked with in this way. And doubles can introduce fractional portions to numbers, reflecting real-life scenarios and adding precision.

And in defining what particular types work with, an appropriate amount of memory can be allotted, avoiding wastage. (Java short requires a lot less memory than a Java long, for example.)