⬅ Previous Topic
VariablesNext Topic ⮕
Complex Data Types⬅ Previous Topic
VariablesNext Topic ⮕
Complex Data TypesPrimitive 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:
true
or false
They are called primitive because they are not composed of other types. They serve as the foundational building blocks for other complex types.
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
25 3.1415 true A Alice
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.
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:
Float
.1
to a Boolean
without explicitly converting it.Primitive types usually have a fixed memory size:
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.
DECLARE a AS Integer = 10
DECLARE b AS Integer = a
SET b = 20
PRINT a
PRINT b
10 20
This shows that a
remains unchanged when b
is modified, demonstrating pass-by-value.
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.
⬅ Previous Topic
VariablesNext Topic ⮕
Complex Data TypesYou can support this website with a contribution of your choice.
When making a contribution, mention your name, and programguru.org in the message. Your name shall be displayed in the sponsors list.