Back to series
· 8 min read

Value Types & Reference Types


I. What Are Value Types and Reference Types?

Value types and reference types are two common categories used in programming languages to describe how data behaves.

At their core, the difference lies in whether, when a variable is assigned or passed, what gets copied is the data itself or a reference to that data.

1. Value Types

A value-type variable directly holds a value. When one value-type variable is assigned to another, the value is usually copied.

The two variables then hold independent values; modifying one does not directly change the other.

For example:

a := 10
b := a

b = 20

fmt.Println(a) // 10
fmt.Println(b) // 20

The assignment process can be abstracted as:

flowchart TD
    A["Variable a<br/>value is 10"] --> B["Copy the value"]
    B --> C["Variable b<br/>value is 10"]
    C --> D["Change b to 20"]

    A1["a is still 10"]
    D --> A1

    style A fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
    style B fill:#E5F4EC,stroke:#A3CBB5,color:#334155
    style C fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
    style D fill:#FFF5DE,stroke:#E8D29A,color:#334155
    style A1 fill:#E5F4EC,stroke:#A3CBB5,color:#334155
    linkStyle default stroke:#A3B8AC,stroke-width:1.5px

Note that a value type does not necessarily mean the data must be stored on the stack, nor does it mean every assignment involves a physical copy of memory.

Value types describe value semantics at the language level, not a specific memory layout.

2. Reference Types

A reference-type variable typically holds a reference to an object. When one reference-type variable is assigned to another, what is usually copied is the reference, not the object itself.

As a result, two variables may point to the same object.

For example:

class User {
    String name;
}

User a = new User();
a.name = "Alice";

User b = a;
b.name = "Bob";

System.out.println(a.name); // Bob
System.out.println(b.name); // Bob

The assignment process can be abstracted as:

flowchart TD
    A["Variable a"] --> C["The same object"]
    B["Variable b"] --> C
    C --> D["Modify the object's field"]
    D --> E["Access via a: Bob"]
    D --> F["Access via b: Bob"]

    style A fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
    style B fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
    style C fill:#F3EDFC,stroke:#C4B2E3,color:#334155
    style D fill:#FFF5DE,stroke:#E8D29A,color:#334155
    style E fill:#E5F4EC,stroke:#A3CBB5,color:#334155
    style F fill:#E5F4EC,stroke:#A3CBB5,color:#334155
    linkStyle default stroke:#A3B8AC,stroke-width:1.5px

The key point here is not that the two variables share the variable itself, but that the references they hold point to the same object.

II. Core Differences Between Value Types and Reference Types

Comparison aspect Value type Reference type
Assignment semantics Usually copies the value Usually copies the reference
Data relationship Usually creates an independent copy May share the same object
Effect of modification Modifying the copy usually does not affect the original variable Modifying a shared object may affect other references
Object identity Usually concerned with whether values are equal Usually also requires checking whether it is the same object
Memory management Depends on the language and implementation Usually involves object lifetime management
Common uses Numbers, booleans, structured values Objects, collections, dynamic data structures

These differences are generalizations of typical behavior; not every language follows exactly the same set of classification rules.

In particular, note the following:

  • Value types may also contain reference fields.
  • Reference types can also be immutable.
  • A value type is not the same as stack allocation.
  • A reference type is not the same as heap allocation.
  • Assigning a reference type is not the same as a deep copy.

III. Assignment: Copying the Value or Copying the Reference?

Assignment is the most important entry point for understanding the two types.

1. Value-Type Assignment

Suppose variable A holds a value, and A is assigned to B:

A = 100
B = A

After the assignment completes, A and B each hold data with the value 100.

flowchart TD
    A["Variable A<br/>100"] --> C["Assignment operation"]
    C --> B["Variable B<br/>100"]
    B --> D["Modify B = 200"]
    D --> E["A = 100"]
    D --> F["B = 200"]

    style A fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
    style B fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
    style C fill:#E5F4EC,stroke:#A3CBB5,color:#334155
    style D fill:#FFF5DE,stroke:#E8D29A,color:#334155
    style E fill:#E5F4EC,stroke:#A3CBB5,color:#334155
    style F fill:#F3EDFC,stroke:#C4B2E3,color:#334155
    linkStyle default stroke:#A3B8AC,stroke-width:1.5px

2. Reference Type Assignment

Suppose variable A references an object, and A is assigned to B:

A ──reference──> Object
B = A

After the assignment, A and B hold references to the same object.

flowchart TD
    A["Variable A"] --> C["Object"]
    B["Variable B"] --> C
    C --> D["Object property: name = Alice"]

    style A fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
    style B fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
    style C fill:#F3EDFC,stroke:#C4B2E3,color:#334155
    style D fill:#E5F4EC,stroke:#A3CBB5,color:#334155
    linkStyle default stroke:#A3B8AC,stroke-width:1.5px

At this point there are two different operations, which must be strictly distinguished.

Modifying the object's contents:

b.name = "Bob";

Since A and B point to the same object, the modification is also visible when accessed through A.

Modifying the variable's own reference:

b = new User();

This makes B point to another object, but does not change A's original reference.

flowchart TD
    A["Variable A"] --> C["Original object"]
    B["Variable B"] --> D["New object"]
    C --> E["Original object continues to exist"]
    D --> F["B points to the new object"]

    style A fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
    style B fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
    style C fill:#F3EDFC,stroke:#C4B2E3,color:#334155
    style D fill:#E5F4EC,stroke:#A3CBB5,color:#334155
    style E fill:#E8EDF2,stroke:#B8C3CE,color:#334155
    style F fill:#FFF5DE,stroke:#E8D29A,color:#334155
    linkStyle default stroke:#A3B8AC,stroke-width:1.5px

IV. Shallow Copy and Deep Copy

The difference between value types and reference types becomes more complicated when copying composite objects.

1. Shallow Copy

A shallow copy creates a new top-level object and copies its field values.

If a field is itself a reference, what gets copied is usually still a reference, so the original object and the new object may share internal objects.

For example:

class Address {
    String city;
}

class User {
    String name;
    Address address;
}

After performing a shallow copy:

User user1 = new User();
user1.name = "Alice";

user1.address = new Address();
user1.address.city = "Singapore";

User user2 = new User();
user2.name = user1.name;
user2.address = user1.address;

At this point:

  • user1 and user2 are two different User objects.
  • The name fields of the two objects are independent string references.
  • The address fields of the two objects point to the same Address object.
flowchart TD
    A["user1"] --> B["User object 1"]
    C["user2"] --> D["User object 2"]
    B --> E["Address object"]
    D --> E

    style A fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
    style C fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
    style B fill:#F3EDFC,stroke:#C4B2E3,color:#334155
    style D fill:#F3EDFC,stroke:#C4B2E3,color:#334155
    style E fill:#E5F4EC,stroke:#A3CBB5,color:#334155
    linkStyle default stroke:#A3B8AC,stroke-width:1.5px

If you modify user2's address.city, user1's address.city changes as well.

But modifying user2.name does not change the reference held by user1.name.

2. Deep Copy

A deep copy not only creates a new top-level object, but also recursively copies the internal objects that need to be owned independently.

user1
  └── Address object 1

user2
  └── Address object 2

The two objects and their internal structures no longer share the mutable objects that need to be copied.

flowchart TD
    A["user1"] --> B["User object 1"]
    C["user2"] --> D["User object 2"]
    B --> E["Address object 1"]
    D --> F["Address object 2"]

    style A fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
    style C fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
    style B fill:#F3EDFC,stroke:#C4B2E3,color:#334155
    style D fill:#F3EDFC,stroke:#C4B2E3,color:#334155
    style E fill:#E5F4EC,stroke:#A3CBB5,color:#334155
    style F fill:#FFF5DE,stroke:#E8D29A,color:#334155
    linkStyle default stroke:#A3B8AC,stroke-width:1.5px

A deep copy does not mean every associated object must be copied. For immutable objects, shared resources, circular references, or structures whose object identity must be preserved, you usually need to define an explicit copying strategy.

3. Shallow Copy vs. Deep Copy

Dimension of comparison Shallow copy Deep copy
Top-level object Creates a new object Creates a new object
Primitive fields Copies the field value Copies the field value
Reference fields Usually copies the reference Copies internal objects according to the strategy
Internal mutable objects May be shared Usually independent
Performance cost Usually lower Usually higher
Implementation complexity Lower Higher

One detail that is easy to overlook: shallow copy and deep copy are not another way of saying value types and reference types. They describe object copying strategies, whereas value types and reference types describe the data semantics of a language.

V. How Value Types and Reference Types Behave in Different Languages

Different programming languages do not design values and references in exactly the same way. You cannot determine which type semantics a language uses merely from whether a variable points to a memory address.

1. Java: Primitive Types and Reference Types

Java clearly distinguishes between primitive data types and reference types.

Primitive data types:

  • byte
  • short
  • int
  • long
  • float
  • double
  • char
  • boolean

A variable of a primitive type stores the corresponding value directly.

int a = 10;
int b = a;

b = 20;

System.out.println(a); // 10

Reference types:

  • Classes
  • Arrays
  • References of interface types
  • Enum types
  • Object types such as String
int[] a = {1, 2, 3};
int[] b = a;

b[0] = 100;

System.out.println(a[0]); // 100

An important characteristic of Java is that all parameter passing is pass by value.

For primitive types, a copy of the primitive value is passed.

For objects, a copy of the reference value is passed.

class User {
    String name;
}

static void change(User user) {
    user.name = "Bob";
}

static void reassign(User user) {
    user = new User();
    user.name = "Charlie";
}

Call:

User u = new User();
u.name = "Alice";

change(u);
System.out.println(u.name); // Bob

reassign(u);
System.out.println(u.name); // Bob

Reason:

  • change receives a copy of the reference, but that copy and the original variable point to the same object, so modifying the object's fields affects the original object.
  • reassign only modifies the reference copy inside the method; the original variable u still points to the original object.

Java does not have pass-by-reference in the traditional sense. The reference itself is copied as a value and then passed into the method.

2. C++: Value Semantics and Reference Semantics Are More Explicit

C++ supports value types, pointers, and references at the same time, allowing explicit control over how parameters are passed.

Pass by value:

void change(int x) {
    x = 20;
}

int main() {
    int a = 10;
    change(a);

    // a is still 10
}

The function receives a copy of the argument; modifying the copy does not affect the caller's variable.

Pass by reference:

void change(int& x) {
    x = 20;
}

int main() {
    int a = 10;
    change(a);

    // a becomes 20
}

int& declares a reference parameter, and the function operates on the caller's original variable.

Pass by pointer:

void change(int* p) {
    if (p != nullptr) {
        *p = 20;
    }
}

int main() {
    int a = 10;
    change(&a);

    // a becomes 20
}

A pointer stores the object's address; by dereferencing it, you can access and modify the target object.

Note that in C++, a reference is not a syntactic alias for an ordinary pointer. Once a reference is bound to an object, it usually cannot be rebound to another object; a pointer, however, can be changed to point elsewhere.

Classes and structs in C++ use value semantics by default, but shared objects can also be implemented through pointers, references, smart pointers, and similar mechanisms.

3. Python: Object Model and Name Binding

Python does not use Java's dichotomy between primitive types and reference types.

In Python, variables are more accurately described as name bindings to objects (Name Binding).

A variable name is bound to an object; assignment usually makes another name bind to the same object as well.

a = [1, 2, 3]
b = a

b.append(4)

print(a)  # [1, 2, 3, 4]
print(b)  # [1, 2, 3, 4]

Here, a and b are both bound to the same list object.

But for integers:

a = 10
b = a

b = 20

print(a)  # 10
print(b)  # 20