0% found this document useful (0 votes)
12 views8 pages

CSC401 2

The document explains the importance of data types in programming languages, categorizing them into primitive, composite, and abstract data types. Primitive data types include basic values like integers and booleans, while composite data types are user-defined structures like arrays and records. Abstract data types provide a theoretical framework for data structures, allowing for various implementations while maintaining consistent behavior.

Uploaded by

aimanazeez02
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views8 pages

CSC401 2

The document explains the importance of data types in programming languages, categorizing them into primitive, composite, and abstract data types. Primitive data types include basic values like integers and booleans, while composite data types are user-defined structures like arrays and records. Abstract data types provide a theoretical framework for data structures, allowing for various implementations while maintaining consistent behavior.

Uploaded by

aimanazeez02
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

DATA TYPES AND STRUCTURES

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

TYPES OF DATA TYPES IN PROGRAMMING LANGUAGES


Programming languages used a variety of data types to handle different kinds of data. Data
types can be grouped into three common categories based on their nature and usage as follows:
1. Primitive Data Types
Primitives are predefined data types that are independent of all other kinds and include basic
values of particular attributes, like text or numeric values. In higher level programming,

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;
};

3. Abstract Data Types (ADTs)


An abstract data type is an abstraction of a data structure that provides only the interface to
which the data structure must adhere. The interface does not give any specific details about
something should be implemented or in what programming language.

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.

List ADT operations:


 add(Object element): Appends the element to the end of the list.
 add(int index, Object element): Inserts the element at specified index.
 remove(int index): Removes the element at specified index.
 remove(Object element): Removes the first occurrence of the specified element.
 size(): Returns the number of elements present in the list.
 get(int index): Returns the element at specified index.
 isEmpty(): Returns true if the list is empty else returns false.

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.

Stack ADT operations:


 push(Object element): Pushes the element to the top of the stack.
 pop(): Removes an element from the top of the stack.
 peek(): Returns the topmost element from the stack but does not remove it.
 isEmpty(): Returns true if the stack is empty, else returns false.
 isFull(): Returns true if the stack is full, else returns false.
 size(): Returns the number of elements present in 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).

Queue ADT operations:


 add(Object element): Insert an element at the rear end of the queue.
 remove(): Removes an element from the front of the queue.
 peek(): Returns the front element of the queue if the queue is not empty, else returns
null.
 size(): Returns the number of elements present in the queue.
 isEmpty(): Returns true if the queue is empty else returns false.
 isFull(): Returns true if the queue is full else returns false.

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.

Note: Let consider some definitions. A type is a set of values.


A data type consists of
 a collection of types, T, one of which is designated as the “primary” type. The data type
is known by the name of its primary type;
 a collection of operations such that for each operand (or result), V, there is a given type
T ϵ T for which T is the collection of admissible values for V.
15
For example, in the stack data type, the collection of types, T is {StackType, ItemType,
boolean). Stack Type is the primary type. The stack operations, whose definitions are
already described above.

16

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy