CSC401 2
CSC401 2
DATA TYPES
In programming languages, data types are critical because they tell the computer how to handle
different kinds of data. Imagine data types as categories in a library. Since a library organizes
books into fiction, non-fiction, or reference. A programming language categorizes data into
user-defined data types such as numbers or text values. These categories help the computer
process data correctly.
For example, without data types, a computer might confuse a date, such as 03/04/2023, with a
mathematical division. By specifying that a piece of data is a date, the computer understands
how to treat it. Data types also help with memory management by allocating space for each
data type.
Data type is the most basic and the most common classification of data; it is an attribute of
data which tells the compiler (or interpreter) how the programmer intends to use the data.
Basically data type is a type of information transmitted between the programmer and the
compiler where the programmer informs the compiler about what type of data is to be stored
and also tells how much space it requires in the memory.
Numerous programming languages, for instance, utilize the data types such as string, integer,
and floating point to represent text, whole numbers, and values with decimal points,
respectively. An interpreter or compiler can determine how a programmer plans to use a given
set of data by looking up its data type.
The data comes in different forms. Examples include:
your name – a string of characters
your age – usually an integer
the amount of money in your pocket- usually decimal type
today's date - written in date time format
9
primitive data types: are the ones not defined in terms of other data types. Some primitive data
types are merely reflections of the hardware while others require only a little non-hardware
support for their implementation. The following are primitive data type:
Boolean type
The Boolean type represents the values true and false. Although only two values are
possible, they are rarely implemented as a single binary digit for efficiency reasons.
Many programming languages do not have an explicit boolean type, instead
interpreting (for instance) 0 as false and other values as true.
Numeric types
The numeric data types consist of the following:
The integer data types or "whole numbers". May be subtyped according to their
ability to contain negative values (e.g. unsigned in C and C++). May also have a
small number of predefined subtypes (such as short and long in C/C++); or allow
users to freely define subranges such as 1..12 (e.g. Pascal/Ada).
Floating point data types, sometimes misleadingly called reals, contain fractional
values. They usually have predefined limits on both their maximum values and
their precision. These are often represented as decimal numbers.
Fixed point data types are convenient for representing monetary values. They are
often implemented internally as integers, leading to predefined limits.
Character types
This is alphanumeric character. It is a letter of the alphabet, digit, blank space, punctuation
mark, etc.
The following example illustrates the use of basic primitive data types in some languages:
Java C++ C#
int myNum = 5; int myNum= 5; int myNum = 5;
float myFloatNum = double myFloatNum double myDoubleNum
5.99f; = 5.99; char myLetter = 5.99D;
char myLetter = = 'D'; char myLetter =
'D'; bool myBoolean = true; 'D';
boolean myBool = string myText bool myBool = true;
true; = "Hello"; string myText =
String myText = "Hello";
"Hello";
10
2. Composite Data Types
These are derived from the primitive data types and they are data types that composed of
primitive or composite types. These data types are made up of various primitive kinds that are
typically supplied by the user. They are also referred to as user-defined or non-primitive data
types. Composing a primitive type into a compound type generally results in a new type such
as array-of-integer which is a different type to integer. These include:
Array stores a number of elements of the same type in a specific order. They
are accessed using an integer to specify which element is required (although
the elements may be of almost any type). Arrays may be fixed length or
expandable.
Records (also called tuple or struct) are among the simplest data structures. A
record is a value that contains other values, typically in fixed number and
sequence and typically indexed by names. The elements of records are
usually called fields or members.
Alphanumeric strings, a sequence of characters. They are typically used to
represent words and text. Consider the following character String Type in
some programming languages:
• C and C++ – Not primitive – Use char arrays and
a library of functions that provide operations
• SNOBOL4 (a string manipulation language) –
Primitive – Many operations, including elaborate
pattern matching
• Fortran and Python – Primitive type with
assignment and several operations
• Java – Primitive via the String class
• Perl, JavaScript, Ruby, and PHP – Provide built-
in pattern matching, using regular expressions
11
The following example illustrates the use of composite data types in some languages:
Java C++ C#
String[] cars = {"Volvo", string cars[4] = string[] cars = {"Volvo",
"BMW", "Ford", "Mazda"}; {"Volvo", "BMW", "Ford" "BMW", "Ford", "Mazda"};
System.out.println(cars[0]); , "Mazda"}; Console.WriteLine(cars[0]
cout << cars[0]; );
Java doesn’t support structures. struct{ struct Books {
The equivalent in Java to a struct int myNum; public string title;
string myString; public string author;
would be } myStructure; // public string subject;
class Member Structure variable public int book_id;
{ };
public String firstName;
public String lastName;
public int birthYear;
};
Any type that does not specify an implementation is an abstract data type. For instance, a stack
(which is an abstract type) can be implemented as an array (a contiguous block of memory
containing multiple values), or as a linked list (a set of non-contiguous memory blocks linked
by pointers). Abstract types can be handled by code that does not know or "care" what
underlying types are contained in them. Arrays and records can also contain underlying types,
12
but are considered concrete because they specify how their contents or elements are laid out in
memory.
Abstract data types are purely theoretical entities, used (among other things) to
simplify the description of abstract algorithms, to classify and evaluate data structures,
and to formally describe the type systems of programming languages.
Abstract Data Type in Java
List ADT
Stack ADT
Queue ADT
List ADT: List ADT stores the similar data type elements in a sequential order. List abstract
data types are dynamic in nature i.e. there is no need to define their size during the declaration.
Note that, the List ADT need to store the required data in the sequence.
13
Stack ADT: A stack stores similar data type elements in an ordered sequence. It can be
imagined as a vertical array in which all the operations of the stack are performed at the top of
the stack.
In Stack ADT, the order of insertion and deletion should be according to the FILO or LIFO
Principle. Elements are inserted and removed from the same end, called the top of the stack.
Queue ADT: A queue stores elements of the same data type sequentially. Operations on the
queue can be performed at both ends i.e front/head and rear end. Insertion operation is done at
rear end and removal operation is done at front end.
14
The Queue ADT follows a design similar to the Stack ADT, but the order of insertion and
deletion changes to FIFO. Elements are inserted at one end (called the rear) and removed from
the other end (called the front).
From the above abstract data types and the operations that can be performed on them, we can
see that the operations do not define how they are implemented internally.
The Stack ADT can be implemented using an array or a linked list. The Queue ADT can be
implemented using an array or a singly linked list or a doubly linked list. Hence an ADT can
be implemented in many different ways but the behavior remains the same.
16