Data Types and Operations

Data Types and Operations


Data Types And Operations On Data

  • Objectives
  • To establish the difference between primitive data types and composite data types.
  • To be able to classify the primitive data types.
  • To differentiate between integral data type and floating point types.
  • To know the data range for each primitive type.
  • To know the storage requirement for each primitive type.
  • To know the conditions for data conversion.
  • To determine assignment assignability.
  • To be able to evaluate arithmetic expressions, relational expressions, and logical expressions.


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 one does not understand data types, data values, and the amount of memory space required for each type of data.

The first half of the lesson introduces you to a comprehensive study of the fundamental data types and how they relate to writing programs in an object oriented form. The lesson begins by introducing us to the two broad categories of data types. These are described as primitive types and composite types. This section of the lesson discusses in detail the primitive types by their names, the amount of memory required to store data for each of them, and the operations that can be performed on each of them. The section closes with the discussion of what expressions are and how they are evaluated.

The second half of the lesson introduces you to the composite data types. The types that are discussed are arrays and classes. A brief introductory remark will be made about the concept of arrays. The lesson concludes with a study of Java’s fundamental classes as composite type.

Data Types

We established in the introduction that data is vital to a program. The data that is to be used by a program must be stored in primary memory in order for the processor to process it. The Java programming language specifies two categories of data values. One type of values is called primitive values. The other type is called composite values. Primitive types are the building blocks of the language. They are indigenous to the language. They do not have to be constructed. Composite types on the other hand are constructed from the primitive types; if they are not provided as supplement to the language, then the programmer will have to construct them.

In the field of computer science, like many other areas of life, conservation is a key ingredient for success; hence the need for the different data types. Elements of each type require different amount of memory space to store the actual data value. For instance, if ten people are going on a journey and want to travel together, it would most likely require the use a minivan instead of a car; or if you want to study the molecular structure of an organism, then you would be better of using a microscope rather than a telescope. In terms of programming the programmer should pay attention to the size of the data value to be stored, and to instruct the machine how much space it must reserve for each data value. This will have a direct impact on the kind of manipulation that can be performed on the data value. Figure 3.1 shows the hierarchy of the data types.

Figure 3.1 Java data types.

Primitive Data Types

The primitive data types in Java falls into three categories. These are the integral types, the floating-point types, and the logical type. The integral type consists of two sub-types. These are the integer data types and the character type. We say integral type because we can represent elements of their kind by integer values.

Integral Types

Integral data values do not have decimal values associated with them; hence, the concept of integral data types. Integral types are divided into two categories – integers and characters. There are four integer types. Their names are 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 3.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 3.2 Java integer data types along with the storage requirements and range of values for each type

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

Character Type

The character data type named char is any printable symbol found on the keyboard or certain sequences of characters forming one piece of instruction to the machine. In either case, each type requires 16 bits (2 bytes) of memory to store the char value. There are other arrangements to store character values. One popular scheme is called the ASCII set. It uses 8 bits (1 byte) store the data. This scheme can represent a maximum of 256 character values, but as you see Java can accommodate 65,536 character values. This arrangement is called the Unicode character set. The Unicode character set can represent a wide variety of ideograms including those form languages other the English language.

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 letters A thru F represents 10 thru 15. Figure 3.3 shows the decimal range and the 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 3.3 Character data type, storage requirements and their decimal and Unicode ranges.

The Unicode character set comprises of ordinary characters and the escape sequence of characters. Ordinary character data value must be placed within single quotation mark. 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 withinthe quotation mark.

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. Figure 3.4 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 3.4 Character escape sequences, their meaning, integer and Unicode representations.

Floating Point Types – float, double

As mentioned earlier, floating-point numbers are primitive types. There are two floating-point data types in Java. These are called float and double. Floating points values are double by default. A numeric value may be appended with f or F to indicate that the number is a floating-point number. For instance, 1024F indicates to Java that this number must be treated as a floating-point number. Hence the numbers 1024.0 and 1024F are both floating-point numbers. Figure 3.5 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 3.5 Java floating-point data types along with storage requirement and range for each type.

Boolean Type

Java’s logical data type is called boolean. The set of values that represent the boolean data type is trueand false. This data type is implemented when relational and logical expressions are to be evaluated.

Declaring And Initializing Variables

In Java every variable must be declared before it can be used. The format for declaring any of these data types is as follows:

data_type identifier;

Where data_type is a valid data type and identifier is the name of the variable representing the type. Each declaration must terminate with a semicolon, unless you are specifying a series of variables, which are all the same type. In this case a comma must separate each variable; the last one must be terminated with a semi-colon.

Numeric Types – Integers and Floating-Point

The following are typical declarations for numeric variables:






After a variable has been declared, it may be assigned a value. The value that is to be assigned must be compatible with the declaration. The format for assigning values is as follows:

destination_Variable = source_variable;

In order to assign a value to an identifier, one of two conditions must be satisfied.

  1. The data value to be assigned must be within the range of the specified data type.
  2. The identifier representing the destination variable must be of equal size or larger than the identifier representing the source variable. If this condition is not satisfied, then we could try and coerce the value to fit the destination variable by a method called casting. If this is done however, then there is a high possibility that the value stored in the destination variable will not be accurate.

For instance, consider the following segment of code:

int x = 2;

byte y = x;


This results in the following syntax error:

-----Configuration: j2sdk1.4.1_02 <Default>----

C:\istings\data_types\ possible loss of precision

found : int

required: byte

byte y = x;


1 error

Process completed.

Now let us coerce the value of x to be a byte:

byte y = (byte)x;

Now this compiles fine and the output generated is 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.

A non-decimal data value is considered to be an integer unless it is qualified to be a long, in which case the letter L or l must be appended to the numeric value. Similarly, a decimal value is by default a double, unless it is qualified by appending the letter F or f to it, in which case the number is converted to fit the space of a float.

ANumber= 6;

firstNumber = 20;

realNumber = 10F;

bigNumber = 2.0E10;

aLongNumber = 2L;

Alternately, a numeric variable may be declared and initialized at once. The following example has the same effect as if we had carried out the operations separately.

byte aNumber = 6;

int firstNumber = 20;

float realNumber = 10F;

doublebigNumber = 2.0E10;

long aLongNumber = 2L;

Character Type

The format for declaring a character variable is the same as the format for numeric variables. However, the method of initializing character differs, in that the value that is being assigned to the identifier must be enclosed within single quotation marks, whether the value is an ordinary value or an escape sequence character. The following are typical ways of declaring and initializing character variables.

char digit_nine = '9';

char another_way= '\u0039';

char tab = '\t';

char newline= '\n';

char uppercase_A = ‘A’;

Boolean Type

The initialization of a boolean variable follows the similar pattern as a numeric variable and character variable. The following is a valid way of declaring and initializing a boolean variable, called more:

boolean more = true;

Where boolean is the data type, moreis the name of the variable, and trueis the data value that is assigned to the variable.

Assignment Incompatibility

A variable can be initialized wrongly. That is, the value that is assigned to it might be of the wrong type. If this happens the compilation process will fail. The following three examples are incompatible, as shown by the compiler error messages.

a)int x = 2.0 ; ----Configuration: j2sdk1.4.1_02 <Default>----

C:\chapter3\ possible loss of precision

found : doublerequired: int

int x = 2.0;


1 error

Process completed.

b)short x = 150000; ----Configuration: j2sdk1.4.1_02 <Default>----

C: \chapter3\ possible loss of precision

found : int required: short

short x = 150000;


1 error

Process completed.

c)boolean x = 0;

----Configuration: j2sdk1.4.1_02 <Default>----

C: \chapter3\ incompatible types

found : int required: boolean

boolean x = 0;


1 error

Process completed.

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

Exercises (3a)

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









  1. 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.

d)Declare a float variable called how_Is_It with 4. as its initial value.

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

  1. 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;

  1. 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.

  1. In question 3, the analyst recommends a digital signaling that operates at 9,600 bits per second, what is the minimum data type would be required to represent the speed of the device? Explain.
  1. 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 to 120 over 75 to 80 be normal reading. If you were to write a program to represent these numbers, what is the smallest data type that could be used to represent these values? Explain.
Operators and Operations On Data

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 operations in this lesson.

Arithmetic Operators

Java defines five binary arithmetic operators and two unary operators. The binary operators are used to form what are called arithmetic expressions. The format of an arithmetic expression is as follows:

operand operator operand;

Where the operands are any valid identifier or actual data value, and operator is any of five arithmetic operators.

The result of an arithmetic expression will generate different type of numeric value, depending on the type of operands. First let us consider the numeric integral types.

Integer arithmetic yields integer result. The first three operators ( +, -, * ) carry out the respective operation as they would be done in ordinary arithmetic. The division ( / ) does not have the usually arithmetic meaning. Instead, it defines the quotient of the division. The remainder is discarded. Similarly the modulus ( % ) defines the remainder from the division process. The quotient is discarded.

In the last two, if division by zero is attempted then the operation will not be carried through. The program would generate what is called ArithmeticException (one word), and terminate, unless special arrangement is made to handle the exception.

The arithmetic operators and their respective operation are summarized in Figure 3. 6.

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 / 18/2 / 9
% / Modulus operation / x % y / 25 % 7 / 4

Figure 3.6 Java’s five arithmetic operators.

Arithmetic Operations on Integers and Floating-Point Types

The operation on byte data values produces in context either a byte data value or an int data value. Listing 3.1 summarizes the result from each of the operations.

class arithmetic_on_type_byte


public static void main(String[] arg)


byte x1 = 60,

x2 = 50,

y1 = 127,

y2 = 127;

System.out.println("byte + byte = byte " + (x1 + x2));

System.out.println("byte + byte = int " + (y1 + y2));

System.out.println("\nbyte - byte = byte " + (x1 - x2));

System.out.println("byte - byte = int " + (-y1 - y2));

System.out.println("\nbyte * byte = byte " + (x1 * 2));

System.out.println("byte * byte = int " + (y1 * y2));

System.out.println("\nbyte / byte = byte " + (y1 / x2));

System.out.println("byte % byte = byte " + (y1 % x2));

System.out.println("\nVerify each");



Listing 3.1 Operations on byte data values.

ListiFigure 3.7 Arithmetic results on byte data.

The operation on short data values produces in context either a short data value or an int data value. Listing 3.2 summarizes the result from each of the operations.

class arithmetic_on_type_short


public static void main(String[] arg)


short x1 = 250,

x2 = 100,

y1 = 32767,

y2 = 1273;

System.out.println("short + short = short " + (x1 + x2));

System.out.println("short + short = int " + (y1 + y2));

System.out.println("\nshort - short = short " + (x1 - x2));

System.out.println("short - short = int " + (-y1 - y2));

System.out.println("\nshort * short = short " + (x1 * 2));

System.out.println("short * short = int " + (y1 * y2));

System.out.println("\nshort / short = short " + (y1 / x2));

System.out.println("short % short = short " + (y1 % x2));

System.out.println("\nVerify each");



Listing 3.2 Operations on short data values.