4.  Data Types and Operations On Data

Objective

·  To understand what data types are

·  To understand the reason for studying data types

·  To differentiate between primitive types and reference types

·  To know the range and storage requirements for each data type

·  To know the criteria for data conversion

·  To know the permissible operations that can be performed on data

·  To be able to evaluate expressions

Introduction

In any programming language the concept of data type is like what the bolts and nuts are to a piece of machinery. It is almost impossible to write any meaningful program, if you do not understand data types, data values, the amount of memory required for each type of data, and the permissible operations that can be performed on data.

Like anything else, conservation of the use of memory is important. In some former programming languages the idea of conserving memory as far as storing data is concerned was rarely considered. Hence, most addressed only two ways to store numeric values – int, for integers; and float, for floating point values. This gave rise to wasting of memory, especially when small values are to be stored; since the amount of memory set aside for each value, large or small, would be the same.

The first section of this lesson introduces you to a comprehensive study of the fundamental data types in Java, and how they relate to programming. It also focuses on the possible operations that can be performed on each type. The second part of the lesson introduces the reference type. This includes some of the fundamental Java classes, such as the String class and the Math class.

Data Types

We had established in the introduction that data is vital to a program. The data that a program uses must be stored in primary memory in order for the processor to handle it. The Java programming language specifies two broad categories of data. One type is called primitive and the other is called reference type. Primitive types are those values that are atomic and therefore cannot be decomposed into simpler types. Reference type on the other hand is constructed from the primitive types as well as from other reference types. Figure 4.1 shows a breakdown of the various categories of the types.

Figure 4.1 Data types - primitive types and reference types

Primitive Type

Let’s say two people are planning on going on a journey and want to travel together, it would be more conservative to use a car instead of a minivan. On average a car uses less petrol; a four-seater car would be half full, and seven-seater minivan would be two-seventh full. Not only that conservation is important, but relevancy as well. Hence, if you want to study the molecular structure of an organism, it would be more relevant to use a microscope rather than a telescope. So much so, it is important to use the appropriate data when programming.

As was mentioned earlier, Java classifies data into primitive type and reference type. The primitive types fall into three categories: the integral types, the floating-point types, and the boolean type. The integral type consists of two sub-types: the integer data types and the character type. Refer to Figure 4.1.

Integral Type

Integral data do not have decimal values associated with them; hence, the concept of integral types. Integral types are divided into two categories – integers and characters. There are four integer types - byte, short, int, and long.

Integers - byte, short, int, long.

The integer types are signed types, meaning that both negative and positive values are considered. Figure 4.2 shows the integer type, the amount of storage that is required for each type, and the range of values that each type can accommodate.

Data types / Storage Required / Range of Values
byte / 8 bits (1 byte) / -128 to +127
short / 16 bits (2 bytes) / -32,768 to +32,767
int / 32 bits (4 bytes) / -2,147,483,648 to +2,147,483,647
long / 64 bits (8bytes) / -9223,372,036,854,775,808 to 9223,372,036,854,775,807

Figure 4.2 Java integer data types, the storage requirements, the and range of values for each type

By default an integer value is an int, unless otherwise specified. For instance, the number 1024 falls within the range of integer values; hence, by default is an int. To specify this number as a long you must append the letter (l or L) to it; as in 1024L, defines 1024, a long. There is no abbreviation for byte or for short.

Floating Point Type

As mentioned earlier, floating-point numbers are primitive types also. Java uses two floating-point types: float and double. A floating point value is double by default. Hence, the number 1024.0 is a double by default. For a decimal literal to be considered a float, you must append the letter f or F to it. That is, 1024F or 1024f the 1024.0 from a double to a float. Figure 4.3 shows the floating-point types, the amount of storage that is required for each type, and the range of values that each type can accommodate.

Data type / Storage Required / Range of Values
float / 32 bits (4 bytes) / -3.4 x 1038 to +3.4 x 1038
double / 64 bits (8 bytes) / -1.7 x 10308 to +1.7 x 10308

Figure 4.3 Java floating-point data types, the storage requirement and range of values for each type

Character Type

The character data type named char is any printable symbol found on the keyboard or certain sequence of characters called escape sequence. In either case, the type char requires 16 bits (2 bytes) of memory to store the char value. Java can accommodate up to 65,536 different characters. A char value can be represented decimal value in the range 0 to 65,536, or as Unicode character in the range ‘\u0000” to ‘\uFFFF’. Unicode characters are usually represented in base 16, where the digits for base 16 are 0 thru 9 and the letters A thru F. The letter as A thru F represents 10 thru 15. Figure 4.4 shows the decimal range and their equivalent Unicode range.

Data Type / Storage Required / Decimal Range / Unicode Range
char / 16 bits (2 bytes) / 0 to 65,536 / \u0000 to \uFFFF

Figure 4.4 Character types, the storage requirements and range of values

Character data value must be placed within single quotation marks. Hence the uppercase letter A, must be written as ‘A’. The digit 5 must be represented as ‘5’. There can be no more than one character within the quotation marks.

A portion of the Unicode character set called escape sequence, is reserved for certain controlling feature. Each escape sequence character is composed of a back slash followed by another character. For instance, we have seen the ‘\t’ for tab, and the ‘\n’ for new line. Figure 4.5 shows a set of escape sequence characters, with their meaning, the decimal equivalence, and the Unicode equivalence.

Escape Sequence / Meaning / Decimal Value / Unicode
'\b' / Backspace / 8 / '\u0008'
'\t' / Tab / 9 / '\u0009'
'\n' / Line feed / 10 / '\u000A'
'\r' / Carriage return / 13 / \u000D'
'\f' / Form feed / 12 / '\u000C'
'\\' / Backslash / 92 / '\u005C'
'\''' / Double quote / 34 / '\u0022'
'\'' / Apostrophe-quote / 39 / '\u0027'

Figure 4.5 Character escape sequences, their meaning, integer and Unicode representations.

Boolean Type

Java’s logical data type is called boolean. The set of values that represent the boolean data type is true and false. This data type is implemented when comparing primitive types. Boolean variables are declared the same way that numeric variables and character variables are declared.

Assignment Incompatibility

A variable can be initialized wrongly. That is, the value that is assigned to it might be of the wrong type. This situation gives rise to syntax errors. Figure 4.6 shows the compiler error messages when an attempt is made to compile the following statement.

int x = 2.0;

Figure 4.6 Error when you assign a floating point value to an integer variable

Figure 4.7 shows the syntax error for the statement: short x = 150000. The value 150000 is out of the range for the type short.

Figure 4.7 Error when value is outside the range of the type

The Boolean data type has only two values – true and false. If a value other than one of these two is assigned to a boolean variable, the result will be syntax error. Figure 4.8 show the syntax error that is generated when attempt is made to compile the following statement:

boolean x = 0;

Figure 4.8 Error - boolean value must be either true or false. No other value.

Consider the following segment of code:

int x = 2;

byte y = x;

This pair of statements looks fine at first glance, but on compiling it we discover that there is a subtle discrepancy which results in syntax error. See Figure 4.9. The second statement does not compile because the variable y is 8 bits wide, and the variable x is 32 bits wide; hence another case of assignment incompatibility.

Figure 4.9 Error due to assignment incompatibility

Let us coerce the value of x to be a byte:

byte y = (byte)x;

This statement compiles fine and y has the correct value, 2. If we change the value of x to 2550 and compile it before the coercion the same compilation error will be generated. When we apply the coercion and execute the code, result is –10.

Notice that in each case the compiler tells you the type of data value you are trying to assign to the variable, followed by what data value is required to be assigned to the variable.

Exercises (4a)

1.  Give the data type for each of the following values. If a value is invalid explain why it is invalid.

a)  1024

b)  1024.0

c)  '\0041'

d)  '\u0041'

e)  '\\'

f)  true

g)  False

2.  For each of the following statements, do the following:

a)  Declare an integer variable called noOfPatrons, with an initial value of zero.

b)  Declare a float variable called why_Is_It with initial value of 4.5.

c)  Declare a float variable called how_Is_It with 4.0 as its initial value.

d)  Repeat (b) and (c), but this time make the variable types be double.

3.  What will happen if you attempt to compile each of the following lines of code?

a)  int x = 25.0;

b)  float x = 1.0;

c)  byte x = 130;

d)  char x = 128;

e)  boolean x = 1;

4.  A company wishes to set up a computer network. The systems analysis proposes one of the following transmission media.

a)  Twisted pair cable with transmission rate of 4 megabits per second.

b)  Coaxial cable with a transmission rate of 500 megabits per second.

c)  Optical cable with transmission rate of 3 gigabits per second.

What is the minimum data type would be required to represent each the speed of each of the transmission medium? Rationalize your answer.

5.  In question 3, the analyst recommends a digital signaling that operates at 9,600 bits per second, what is the minimum data type required to represent the speed of the device? Explain.

6.  A medical facility wishes to replace the current electronic monitoring device for measuring blood pressure. The current instrument gives normal reading to be 120/80 (read 120 over 80). The facility is requesting a range of 115/75 to 120/80 be normal reading. If you were to write a program to represent these numbers, what is the most appropriate data type to represent this range of values? Explain.

Operator and Operations on Data

Computers are known as number crunching machines. In order for them to crunch numbers, they need to perform operations on the numbers. In Java there are five types of operations that can be performed on primitive data values. These are arithmetic operations, relational operations, logical operations, bit-wise operations, and bit-shift operations. We will consider only the first three.

Arithmetic Operator and Operations

Java defines five binary arithmetic operators that are used to form arithmetic expressions. The format of an arithmetic expression is:

operand operator operand;

Where the operands are any valid identifier or literal numeric value, and operator is any of five arithmetic operators. The arithmetic operators and their respective operations are summarized in Figure 4.10.

Operator / Name / Algebraic form / Example / Result
+ / Addition / x + y / 25 + 15 / 40
- / Subtraction / x + y / 25 - 15 / 10
* / Multiplication / x * y / 18 * 2 / 36
/ / Division / x / y / 37/5 / 7
% / Modulus operation / x % y / 25 % 7 / 4

Figure 4.10 Java’s five arithmetic operators

The first three operators (+ , - , * ) have the usual arithmetic meaning. The fourth operator ( / ), gives the quotient when applied to division. That is why 37/5 = 7, and not 7.4

The last operator ( % ), gives the remainder when applied to division. Hence, 37%5 = 2.

You may ask what the applicability of these two operators is. Consider for a moment the following situation:

If a task takes a worker 127 minutes to complete, how many hours and how many minutes did it take the person to complete.

If we were to program this, we would have to tell the computer precisely how to carry out the calculation. That is, the number of hours would be 127/60 = 2 hours, and 127%60 = 7 minutes.

Example

A small company wants you to write a program to figure out the number of boxes needed to ship book orders without wasting space. They have four types of boxes: extra large, large, medium and small, which can hold 25, 15, 5 and 1 book(s) respectively.