What are Primitive Data Types?
Primitive data types are the most basic data types available in programming. These types represent single values and are typically built into the language itself. They are usually stored directly in memory and do not have methods or properties like objects or complex data structures.
Common primitive data types include:
- Integer – Whole numbers like 1, -5, 100
- Float – Numbers with decimal points like 3.14, -0.99
- Boolean – Logical values:
true
orfalse
- Character – A single letter or symbol like 'A', '#'
- String – A sequence of characters, although in some languages this is considered non-primitive
Why Are They Called "Primitive"?
They are called primitive because they are not composed of other types. They serve as the foundational building blocks for other complex types.
Example 1: Declaring and Printing Primitive Values
DECLARE age AS Integer = 25
DECLARE pi AS Float = 3.1415
DECLARE isActive AS Boolean = true
DECLARE grade AS Character = 'A'
DECLARE name AS String = "Alice"
PRINT age
PRINT pi
PRINT isActive
PRINT grade
PRINT name
Output:
25 3.1415 true A Alice
Intuition Check
Question: Why do we need different data types like Integer, Float, and Boolean?
Answer: Different types represent different kinds of information. For example, using a Boolean
to represent a yes/no decision saves memory and clearly indicates intent, whereas using a Float
helps represent values with decimals like measurements or currency.
Example 2: Type Safety and Incompatible Assignments
DECLARE price AS Float = "twenty dollars" // Invalid
DECLARE isValid AS Boolean = 1 // Often invalid without conversion
In many programming languages, the above code will raise an error because:
- You cannot assign a string like "twenty dollars" to a
Float
. - You cannot assign a number like
1
to aBoolean
without explicitly converting it.
Memory Characteristics
Primitive types usually have a fixed memory size:
- Integer: 4 bytes (32-bit) or 8 bytes (64-bit)
- Float: 4 bytes
- Boolean: 1 byte or less
- Character: 1-2 bytes depending on encoding
Since they're stored directly in memory, they are passed by value in most languages. This means when a primitive variable is copied, a new independent copy of the value is created.
Example 3: Copying a Primitive Value
DECLARE a AS Integer = 10
DECLARE b AS Integer = a
SET b = 20
PRINT a
PRINT b
Output:
10 20
This shows that a
remains unchanged when b
is modified, demonstrating pass-by-value.
Summary
- Primitive types are simple, memory-efficient, and language-built data types.
- They include integers, floating-point numbers, booleans, characters, and sometimes strings.
- They are passed by value, not by reference.
- Understanding them helps build a strong foundation in programming.
Quick Questions to Reinforce Understanding
Q: Can we store a decimal value in an integer variable?
A: No. An integer variable can only store whole numbers.
Q: What is the default value of a Boolean in most languages?
A: Typically false
.
Q: Is a String always a primitive data type?
A: No. In some languages, it's treated as a complex object even though it behaves like a primitive in usage.