# Difference Between Primitive and Non Primitive Data Structure

Learn via video course
FREE
View all courses
DSA Problem Solving for Interviews using Java
by Jitender Punia
1000
4.9
DSA Problem Solving for Interviews using Java
by Jitender Punia
1000
4.9

Data Structures are of two types: primitive and non primitive data structures.

Refer to the below image for the types of the data structures

The first type of data structure i.e. primitive data structure is considered as the fundamental data structure and it allows storing the values of only one data type but non-primitive data structures are considered as the user-defined structure that allows storing values of different data types within one entity.

The primitive data structure consists of fundamental data types like float, character, integer, etc.

Let us understand primitive data structure with an example. A variable with the data type integer can allow storing the values of integer type only, a variable with the float data type can allow storing the values of float data type and the variable with the character data type allows storing character values only.

The non-primitive data structure is further classified into two parts i.e. linear data structure and non-linear data structure. Linear data structures are considered sequential data structures and sequential data structures store elements in a sequence in the memory. For instance, the second element of the data structure is stored after the first element and then after the second element, the third element of the data structure is stored and the fourth element is stored after the third and so on. Linked List, Array, and Stack are some examples of linear data structure which stores the elements sequentially. The non-linear data structure is considered as a random type of data structure. Graphs and trees are examples of non-linear data structures.

The main difference between primitive and non-primitive data structures is that Primitive data structures are considered as the fundamental data structure while non-primitive data structures are defined by the user with the help of primitive data structures.

## Difference Between Primitive and Non-Primitive Data Structure

The difference between primitive and non primitive data structure is explained below:

Primitive data structureNon Primitive Data structure
Primitive data structure is the data structure that allows you to store only single data type values.Non-Primitive data structure is a data structure that allows you to store multiple data type values.
integer, boolean, character, float, etc. are some examples of primitive data structures.Array, Linked List, Stack, etc. are some examples of non-primitive data structures.
Primitive data structure always contains some value i.e. these data structures do not allow you to store NULL values.You can store a NULL value in the non-primitive data structures.
The size of the primitive data structures is dependent on the type of the primitive data structure.The size of the non-primitive data structure is not fixed.

## Primitive Data Structures

Refer to the below image for the types of the primitive data structures

Primitive data structures allow storing only one value at a particular location. Primitive data structures of any programming language are the data types that are predefined in that programming language. The type and size of those data types are defined already. The four main primitive data structures which are defined in mostly all programming languages are float, character, boolean, and integer. Other than that, a total of eight data types are defined for the primitive data structure i.e. long, float, byte, char, boolean, integer, double, and short.

Let us look at the primitive data types with their examples:

boolean:- The boolean data type allows storing two values only i.e. true and false. Mostly boolean data type is used for testing the conditions.

Example:

C++

Java

Python

byte:- The byte data type is an 8-bit signed 2's complement integer. The byte data type stores a 1-byte value and the range of this data type is from -128 to 127.

Example:

C++

Java

char:- The character data type allows you to store a single character. It stores ASCII assigned values of the lower case alphabets, upper case alphabets and some special symbols such as @,;, etc.

Example:

C++

Java

short:- The size of the short data type is greater than the size of the byte but smaller than the size of the integer. It allows storing values in the range from -32,768 to 32,767.

Example:

C++

Java

integer:- Integer are used to store the value of the numeric type. It allows for storing both negative and positive values. The range of the integer data type is -2,147,483,647 to 2,147,483,647.

Example:

C++

Java

Python

long:- The long data type is used to store the value in cases where the value is outside the range of the integer data type. The range of the long data type is from $-2^{63}$ to $2^{63}-1$ which is greater than the range of the integer data type.

Example:

C++

Java

float:- The float data type allows you to store the floating value. The float data type ended with the f.

Example:

C++

Java

Python

double:- The double data type also allows you to store the floating value. But float and double differ from each other in terms of their range, float allows to 32-bit value while double allows a 64-bit value.

Example: C++

Java

## Non-Primitive Data Structures

Non-primitive data structures are the data structure created by the programmer with the help of primitive data structures. Non-primitive data structures are divided into linear and non-linear data structures. Arrays and linked lists are examples of linear data structure and trees, graph are examples of non-linear data structure.

Array:- Array is a linear data structure that stores the elements of similar data types at a contiguous memory location.

Example: Refer to the below image for the example of the array data structure

C++

Java

Stack:- Stack is a linear data structure that works on the principle of LIFO(Last In First Out) which allows insertion and deletion of elements from one end only i.e. top.

Example: Refer to the below image for the example of the stack data structure

Queue:- Queue is a linear data structure that works on the principle of FIFO(First In First Out) which allows the insertion of elements at one end i.e. rear and deletion of elements from another end i.e. front.

Example: Refer to the below image for an example of the queue data structure

Tree:- Tree is a non-linear data structure that stores the element as nodes in the form of a hierarchical (parent-child) relationship.

Example: Refer to the below image for the example of the tree data structure