Value Type and Reference Type
Value Type and Reference Type
Learn how value types and reference types store data differently in memory, how assignment works, and why changing one variable may or may not affect another variable.
Introduction
In programming, variables store data. But not all variables store data in the same way.
Some variables store the actual value directly. These are commonly known as value types.
Other variables store a reference, or address-like link, to where the actual data is stored. These are commonly known as reference types.
Understanding value types and reference types helps students understand memory, assignment, object behavior, copying, function arguments, and common programming bugs.
Easy Real-Life Example
Value Type as a Photocopy
Imagine you have a paper with the number 50 written on it. If you photocopy that paper and give it to your friend, your friend has a separate copy.
If your friend changes their copy from 50 to 80, your original paper still remains 50.
Original paper: 50
Copied paper: 50
Friend changes copied paper to 80
Original paper: 50
Copied paper: 80
This is similar to a value type. A copy is independent.
Reference Type as a House Address
Now imagine two people have the same house address written on paper. Both papers point to the same house.
If one person goes to that house and paints the door blue, the other person will also see the blue door because both are referring to the same house.
Address paper 1 → House A
Address paper 2 → House A
Door color changed using address paper 1
Address paper 2 still points to the same House A
So it also sees the changed door color
This is similar to a reference type. Multiple variables may refer to the same object.
What is a Value Type?
A value type is a data type where the variable stores the actual value directly.
When we copy a value type variable into another variable, a new copy of the value is created.
Example: Value Type Assignment
/*
This example shows value type behavior.
*/
ENTRY POINT
DECLARE a AS INTEGER = 10
DECLARE b AS INTEGER = a
SET b = 20
DISPLAY a
DISPLAY b
END ENTRY POINT
Expected Output
10
20
Here, changing b does not change a because b receives a copy of the value.
Common Examples of Value Types
Exact value types depend on the programming language, but common examples often include simple primitive values.
| Value Type Example | Meaning | Sample Value |
|---|---|---|
| Integer | Whole number | 10 |
| Decimal / Float | Number with fractional part | 99.5 |
| Boolean | True or false value | true |
| Character | Single character | 'A' |
| Enum | Named fixed set of values | MONDAY, ACTIVE |
| Struct / Record-like simple value | Small grouped data in some languages | Point(x, y) |
What is a Reference Type?
A reference type is a data type where the variable does not directly store the full object data. Instead, it stores a reference to the actual data.
A reference can be understood as a link, address, or pointer-like value that tells the program where the actual object is located in memory.
Example: Reference Type Assignment
/*
This example shows reference type behavior.
*/
CLASS Student
PROPERTY name
END CLASS
ENTRY POINT
CREATE student1 AS Student
SET student1.name = "Aman"
SET student2 = student1
SET student2.name = "Riya"
DISPLAY student1.name
DISPLAY student2.name
END ENTRY POINT
Expected Output
Riya
Riya
Here, student1 and student2 refer to the same object. So changing the object using student2 is visible through student1.
Common Examples of Reference Types
Exact reference types depend on the programming language, but common examples often include objects and collections.
| Reference Type Example | Meaning | Sample Idea |
|---|---|---|
| Class Object | An instance of a class | Student, Product |
| Array | Collection of values | [10, 20, 30] |
| List | Dynamic collection | studentsList |
| Map / Dictionary | Key-value collection | {"name": "Aman"} |
| Object | Data structure with properties and behavior | car.color, user.email |
| Function / Delegate-like reference | Reference to executable behavior in some languages | callbackFunction |
Value Type vs Reference Type
| Feature | Value Type | Reference Type |
|---|---|---|
| Stores | Actual value. | Reference to actual object. |
| Copy Behavior | Creates a separate copy. | Copies the reference, not the full object. |
| Changing Copy | Original usually does not change. | Original object may appear changed if both variables refer to same object. |
| Common Examples | Numbers, booleans, characters. | Objects, arrays, lists, dictionaries. |
| Memory Idea | Often simple and directly stored. | Reference points to object data elsewhere. |
| Main Risk | Large values may be expensive to copy. | Shared object changes can create unexpected bugs. |
Value Type, Reference Type, Stack, and Heap
Beginners often learn value types and reference types together with stack and heap.
A simplified beginner model is:
Simple Memory Model
- Value type variables often directly hold their data.
- Reference type variables often hold a reference to object data.
- Local variables and function calls are commonly associated with stack memory.
- Dynamically created objects are commonly associated with heap memory.
Value Type:
x = 10
Variable x directly stores 10
Reference Type:
studentRef → Student object in memory
Variable studentRef stores a reference
Actual student data is stored elsewhere
Copying Value Types
When a value type is copied, the value itself is copied.
/*
Copying value type.
*/
ENTRY POINT
DECLARE x AS INTEGER = 5
DECLARE y AS INTEGER = x
SET y = 100
DISPLAY x
DISPLAY y
END ENTRY POINT
Expected Output
5
100
The value of x remains 5 because y has its own separate copy.
Copying Reference Types
When a reference type is copied, usually the reference is copied, not the full object.
/*
Copying reference type.
*/
CLASS Box
PROPERTY value
END CLASS
ENTRY POINT
CREATE box1 AS Box
SET box1.value = 5
SET box2 = box1
SET box2.value = 100
DISPLAY box1.value
DISPLAY box2.value
END ENTRY POINT
Expected Output
100
100
Both box1 and box2 point to the same object, so changing the object through one reference is visible through the other.
Visual Explanation
Value Type Copy
a = 10
b = a
Memory idea:
a → 10
b → 10
Change b = 20
a → 10
b → 20
Reference Type Copy
student1 → Object A
student2 = student1
Memory idea:
student1 ─┐
├──→ Object A { name: "Aman" }
student2 ─┘
Change student2.name = "Riya"
student1 ─┐
├──→ Object A { name: "Riya" }
student2 ─┘
Example: Value Type in Function
When a value type is passed to a function, many languages pass a copy by default.
/*
Changing copy inside function does not change original.
*/
FUNCTION increase(number)
SET number = number + 1
DISPLAY "Inside function: " + number
END FUNCTION
ENTRY POINT
DECLARE count AS INTEGER = 10
CALL increase(count)
DISPLAY "Outside function: " + count
END ENTRY POINT
Expected Output
Inside function: 11
Outside function: 10
The function changed its local copy, but the original variable outside the function remained unchanged.
Example: Reference Type in Function
When a reference type is passed to a function, the function may receive a copy of the reference. That copied reference can still point to the same object.
/*
Changing object property through reference affects the same object.
*/
CLASS User
PROPERTY name
END CLASS
FUNCTION changeName(userRef)
SET userRef.name = "Updated Name"
END FUNCTION
ENTRY POINT
CREATE user AS User
SET user.name = "Original Name"
CALL changeName(user)
DISPLAY user.name
END ENTRY POINT
Expected Output
Updated Name
The function changed the object that both references point to.
Important Difference: Modifying Object vs Reassigning Reference
Beginners often confuse modifying an object with reassigning a reference.
Modifying Object
FUNCTION updateStudent(student)
SET student.name = "New Name"
END FUNCTION
This changes the object’s data.
Reassigning Reference
FUNCTION replaceStudent(student)
CREATE anotherStudent AS Student
SET anotherStudent.name = "Another"
SET student = anotherStudent
END FUNCTION
In many languages, reassigning the local reference parameter may not replace the original caller’s variable. The local parameter starts pointing somewhere else, but the original variable may still point to the old object.
Example: List as Reference Type
Lists are commonly reference-type-like structures in many languages.
/*
Two variables refer to the same list.
*/
ENTRY POINT
DECLARE list1 AS LIST = [10, 20]
SET list2 = list1
ADD 30 TO list2
DISPLAY list1
DISPLAY list2
END ENTRY POINT
Expected Output
[10, 20, 30]
[10, 20, 30]
Since both variables refer to the same list, adding to one is visible through the other.
Shallow Copy and Deep Copy
When working with reference types, programmers sometimes need to create a real copy of the object instead of copying only the reference.
| Copy Type | Meaning | Beginner Explanation |
|---|---|---|
| Reference Copy | Copies only the reference. | Both variables point to the same object. |
| Shallow Copy | Copies the outer object, but nested objects may still be shared. | Partly copied, but some inner references may be shared. |
| Deep Copy | Copies the object and nested objects. | Creates a truly independent copy. |
Reference Copy:
student2 = student1
Deep Copy Idea:
student2 = copy all values from student1 into a new independent object
Value Equality vs Reference Equality
Equality can also behave differently with value types and reference types.
| Equality Type | Meaning | Example Idea |
|---|---|---|
| Value Equality | Compares actual values. | 10 == 10 is true. |
| Reference Equality | Compares whether two references point to the same object. | Two variables point to same student object. |
Some languages allow reference types to define custom equality rules, so students should check the rules of the language they are learning.
Special Case: Immutable Reference Types
Some reference types may be immutable. Immutable means the object cannot be changed after it is created.
In many languages, strings behave like reference-type objects but are immutable.
text1 = "Hello"
text2 = text1
text2 = "World"
DISPLAY text1
DISPLAY text2
Possible Output
Hello
World
This can confuse beginners because strings may act differently from mutable objects. Reassigning text2 may create or point to a different text value instead of changing the original string.
When to Use Value Types
Value types are usually useful for small, simple, independent data.
Use Value Types For
- Numbers.
- Boolean values.
- Small fixed data.
- Values that should not share changes.
- Simple calculations.
- Data where copying is safe and expected.
When to Use Reference Types
Reference types are usually useful for complex, large, shared, or dynamically created data.
Use Reference Types For
- Objects with multiple properties.
- Large data structures.
- Lists, arrays, maps, and dictionaries.
- Data that should be shared across functions.
- Objects that represent real-world entities.
- Data that must live beyond one small scope.
Performance Considerations
Value types and reference types can affect performance differently.
General Performance Ideas
- Small value types are usually efficient to copy.
- Large value types may become expensive if copied frequently.
- Reference types avoid copying large objects, but may require heap allocation.
- Heap allocation and garbage collection may add overhead in some languages.
- Shared reference objects can reduce copying but may create mutation bugs.
Common Beginner Mistakes
Mistakes
- Thinking assignment always creates a full copy.
- Changing a reference-type object and being surprised that another variable sees the change.
- Confusing reference copy with deep copy.
- Assuming all reference types behave like mutable objects.
- Forgetting that strings may be immutable in many languages.
- Passing an object to a function and accidentally modifying it.
- Using shared lists or dictionaries without understanding side effects.
- Thinking value type and reference type rules are identical in every language.
Better Habits
- Ask whether the variable stores the value or a reference.
- Use copies when you do not want shared changes.
- Be careful when modifying objects inside functions.
- Understand mutable and immutable objects.
- Use meaningful names to show when data is shared.
- Learn how your chosen language handles assignment and function arguments.
- Use deep copy when a fully independent object is required.
- Test object-copying behavior with small examples.
Best Practices
Recommended Practices
- Use value types for simple independent values.
- Use reference types for complex objects and collections.
- Avoid unnecessary sharing of mutable objects.
- Do not modify objects inside functions unless that is clearly intended.
- Create copies when original data should remain unchanged.
- Understand how assignment works in your programming language.
- Understand how function parameters are passed in your programming language.
- Be careful with nested objects and shallow copies.
- Prefer immutability when accidental changes are risky.
- Write small experiments to verify behavior before using complex structures.
Prerequisites Before Learning Value and Reference Types
Students should understand the following topics before learning this concept deeply:
Required Knowledge
- Variables and constants.
- Data types.
- Assignment operator.
- Functions and parameters.
- Arrays and lists.
- Objects and classes.
- Stack and heap concept.
- Basic memory idea.
- Mutable and immutable data basics.
Trace Table Example: Value Type
a = 10
b = a
b = 20
| Step | Action | a | b |
|---|---|---|---|
| 1 | Create a |
10 |
Not created |
| 2 | Copy a into b |
10 |
10 |
| 3 | Change b |
10 |
20 |
Trace Table Example: Reference Type
student1.name = "Aman"
student2 = student1
student2.name = "Riya"
| Step | Action | student1 | student2 | Actual Object Name |
|---|---|---|---|---|
| 1 | Create object | Points to Object A | Not created | Aman |
| 2 | Copy reference | Points to Object A | Points to Object A | Aman |
| 3 | Change through student2 |
Points to Object A | Points to Object A | Riya |
Practice Activity: Identify Value or Reference Behavior
Identify whether the behavior is value-type-like or reference-type-like.
1. Copying number x into y and changing y does not change x.
2. Copying list1 into list2 and adding an item to list2 also shows the item in list1.
3. Passing an integer to a function and changing it inside the function does not change the original.
4. Passing a student object to a function and changing student.name changes the original object.
5. Copying a dictionary variable copies the reference to the same dictionary.
Sample Answers
1. Value-type-like behavior
2. Reference-type-like behavior
3. Value-type-like behavior
4. Reference-type-like behavior
5. Reference-type-like behavior
Mini Quiz
What is a value type?
A value type is a type where the variable stores the actual value directly.
What is a reference type?
A reference type is a type where the variable stores a reference to the actual object data.
What happens when a value type is copied?
A separate copy of the value is usually created.
What happens when a reference type is copied?
The reference is usually copied, so both variables may point to the same object.
Why can reference types cause unexpected changes?
Because multiple variables can refer to the same object, changing the object through one variable may be visible through another.
Interview Questions on Value Type and Reference Type
Explain value type with an example.
A value type stores the actual value directly. For example, if a = 10 and b = a, changing b does not change a.
Explain reference type with an example.
A reference type stores a reference to an object. If two variables refer to the same object, changing the object through one variable may affect what the other variable sees.
What is the main difference between value type and reference type?
Value types copy actual data, while reference types copy references to data.
What is a deep copy?
A deep copy creates a fully independent copy of an object and its nested data.
Why is understanding reference type important?
It is important because shared references can cause unexpected changes if multiple parts of a program modify the same object.
Quick Summary
| Concept | Meaning |
|---|---|
| Value Type | Stores the actual value directly. |
| Reference Type | Stores a reference to actual object data. |
| Value Copy | Creates an independent copy. |
| Reference Copy | Copies the reference to the same object. |
| Mutable Object | An object whose internal data can be changed. |
| Immutable Object | An object whose data cannot be changed after creation. |
| Shallow Copy | Copies outer structure but may share nested objects. |
| Deep Copy | Creates a fully independent copy of object and nested data. |
Final Takeaway
Value types and reference types explain how variables store and share data in memory. Value types usually store actual data and create independent copies during assignment. Reference types store references to objects, so multiple variables may point to the same object. In the Memory and Resource Management Basics module, students should understand this concept clearly because it explains many real-world programming behaviors, especially copying, function parameters, objects, lists, dictionaries, mutation, and memory-related bugs.