Understanding
Primitive Data Types



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:

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:

Memory Characteristics

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.

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

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.



Welcome to ProgramGuru

Sign up to start your journey with us

Support ProgramGuru.org

Mention your name, and programguru.org in the message. Your name shall be displayed in the sponsers list.

PayPal

UPI

PhonePe QR

MALLIKARJUNA M