Primitive Data Structure
Overview
The data structures are nothing but a meaningful way of arranging, and storing the data in the computer for efficient utilization and processing depending upon the situation. A primitive data structure can store the value of only one data type. For example, a char data structure (a primitive data structure) can store only characters. The size of a primitive data structure is known as it can store can only one data type. Examples of the primitive data type are integer, character, boolean, float, double, long, etc.
Takeaways
Data types can be classified into two categories:
- Primitive Data Structures
- Non-primitive Data Structures
Cheatsheet for Primitive Data Structures
Before learning about the primitive data structure, let us first learn about data structures and data types.
Data types are nothing but the classification or categorization of the data items. Data types represent the kind of value and also tell what operations can be performed on a particular data. Various programming languages and various data types. On the other hand, the data structures are nothing but a meaningful way of arranging, and storing the data in the computer for efficient utilization and processing depending upon the situation.
Note: The data structures and data types are often used interchangeably.
The data structure can be divided into two categories namely - primitive data structure and non-primitive data structure.
Refer to the diagram below to see the visual representation of the various data types along with their sizes.
Let us briefly discuss the primitive data structures and non-primitive data structures.
Primitive Data Structures: A primitive data structure can store the value of only one data type. For example, a char data structure (a primitive data structure) can store only characters.
Key features of a primitive data structure:
- The size of a primitive data structure is known as it can store can only one data type.
- The primitive data structure will always store some data. It cannot contain the NULL value.
- Examples of the primitive data type are integer, character, boolean, float, double, long, etc.
Non-primitive Data Structures: Unlike the primitive data structure, a non-primitive data structure can store the value of more than one data type. For example, a list in Python can store various data types inside it.
Key features of a primitive data structure:
- The size of a primitive data structure depends upon the type of data it stores.
- The primitive data structure will be just initialized without storing any data or value. It can contain the NULL value.
- Examples of the non-primitive data types are linked lists, stacks, queues, dictionaries, etc.
Now let us learn about the most commonly used primitive data structures along with their examples.
Bool
Bool or boolean is a primitive data structure that can be used to store only two values i.e. True or False. The size of a boolean data type is 1 byte but it only uses 1 bit of the 8 bits(1 byte = 8 bits) to store the value. A bool data type is used when we want to track whether a condition is true or false.
Example: Python
C++
Java
Byte
A byte is a primitive data structure that is used to store 1-byte variables. The size of a byte data type is 1 byte.
Example: Java
Note: The byte data type is an 8-bit signed two's complement integer.
Char
Character or char is a primitive data type that can be used to store the value of a single character (it can store lowercase letters as well as uppercase letters). The size of a character data type is 2 bytes.
Example: C++
Java
Int
Integer or int is also a primitive data type that can be used to store the numbers in the range: to or (2147483648 to 2147483647). The size of a character data type is 4 bytes.
Example: C and C++
Java
Python
Float
A floating-point number or float is a primitive data type that can be used to store fractional numbers.its range is : 3.4e−038 to 3.4e+038. The size of a character data type is 4 bytes.
Example: C and C++
Java
Python
Note: There are two types of floating numbers. The float32 is a 32-bit number and the float64 is a 64-bit number.
Double
Double is also a primitive data type that can be used to store fractional numbers. Its range is 1.7e−308 to 1.7e+308. The size of a character data type is 8 bytes. We need to add d at the end of the number to denote it as a double number in java.
Example: C and C++
Java
Note: Python does not have an inbuilt double data type.
Long
Long is a primitive data type that can be used to store the numbers in the range: to . The size of a long data type is 8 bytes. When we have a number larger than the range of integer then we use the long data type.
Example: C and C++
Java
Short
Short is a primitive data type that can be used to store the numbers in the range: to . The size of a short data type is 2 bytes. When we have e smaller number than the range of integer then we can use a short data type as it takes only 2 bytes.
Example: C and C++
Java
Note: In C++, short is equivalent to short int.
Pointer
Pointer can also be referred to as a primitive data type that can be used to store the address of another variable. Pointers can store the address of other primitive and non-primitive data types as well. Pointer is supported by languages like C and C++. Languages like Java and Python have references as a counterpart of pointers.
C and C++ Example:
Note: Pointer takes 8 bytes of memory.
Refer to the table below to find the range and size of the primitive data structure according to a 64-bit compiler.
Data Type | Size in 64-bit compilers | Range |
---|---|---|
Bool | 1 byte | true or false |
Byte | 1 byte | -128 to 127 |
Char | 2 bytes | 0 to |
Int | 4 bytes | to |
Float | 4 bytes | -3.4E+38 to +3.4E+38 |
Double | 8 bytes | -1.7E+308 to +1.7E+308 |
Long | 8 bytes | to |
Short | 2 bytes | to |
Conclusion
- The data structures are nothing but a meaningful way of arranging, and storing the data in the computer for efficient utilization and processing depending upon the situation.
- The size of a primitive data structure is known as it can store can only one data type. Examples of the primitive data type are integer, character, boolean, float, double, long, etc.
- Bool is a primitive data structure that can be used to store only two values i.e. True or False. A bool data type is used when we want to track whether a condition is true or false.
- byte is a primitive data structure that is used to store 1-byte variables. char is a primitive data type that can be used to store the value of a single character.
- int is a primitive data type that can be used to store the numbers in the range: to .
- float is a primitive data type that can be used to store fractional numbers. Double is also a primitive data type that can be used to store fractional numbers. Its range: 1.7e−308 to 1.7e+308.
- long is used to store the numbers in the range: to . short is used to store the numbers in the range: to .
- Pointer can also be referred to as a primitive data type that can be used to store the address of another variable. Pointers can store the addresses of other primitive and non-primitive data types as well.