Work when you want, how you want, wherever you want in the world.

Mutable vs Immutable Data Types

Jul 31, 2023

Man motioning to keep quiet.

In programming, the concepts of immutable and mutable refer to the state of objects and data structures, indicating whether they can be changed after creation or not. These concepts are particularly important in languages that support object-oriented programming and have variables that can reference data or objects.

Immutable

An object or data structure is considered immutable if its state cannot be modified after it is created. Once an immutable object is created, any attempt to modify its content will result in the creation of a new object with the modified values, leaving the original object unchanged. In other words, immutable objects retain their original values throughout their lifetime.

Examples of immutable data types include strings in many programming languages like Python and Java. When you perform operations like concatenation on a string, a new string is created with the combined values, and the original strings remain unmodified.

Integers are another typical example of an immutable data type. In JavaScript, Java, and Python you cannot change the value of an integer once it is created. It may seem that you are changing an integer's value when you assign a new value to an int variable, but what is actually happening is that a new integer is created using the new value. The initial value is unchanged in memory. The same thing occurs when you attempt to update a string with a new value. The initial value remains unchanged and a new string is created. To illustrate this, let's look at a JavaScript string example:

    let str = "Hello";
    let originalString = str;

    str = str + " World";

    console.log(str); // Output: "Hello World"
    console.log(originalString); // Output: "Hello"
    console.log(Object.is(str, originalString)); // Output: false

    // The original string "Hello" remains unchanged.

In this example, we assign the value of the original string str to a new variable called originalString. After performing the string concatenation, we compare the two strings using Object.is(). As you can see from the output, the result of Object.is() is false, indicating that the two variables reference different string objects in memory. This confirms that the original string "Hello" remains unchanged, and the concatenation operation created a new string "Hello World".

Immutable more so refers to memory, because for all intents and purposes, if you assign a string to a variable and then assign a different string to the same variable name, it appears to the naked eye that the variable string was modified, but in fact, you just created a new object in memory using the same variable name.

When an object is immutable, it means that its internal state cannot be modified after creation. As a result, any operation that appears to modify the object's content actually creates a new object with the updated value, while the original object remains unchanged.

In the case of strings, if you assign a new string to the same variable, it might appear like the variable's value is changing, but in reality, a new string object is created with the updated value, and the variable is made to reference this new object in memory.

This behavior is essential for ensuring data integrity and predictability in programming. Immutable objects offer benefits like thread safety, ease of debugging, and reduced chances of unexpected side effects. When working with immutable objects, you can be confident that their state won't change unexpectedly, simplifying program flow and reasoning about code behavior.

Mutable

In contrast, a mutable object or data structure is one that can be modified or changed after it is created. Any changes made to a mutable object affect its original state, and there is no need to create new instances when modifying the object's properties.

Examples of mutable data types include lists in Python and arrays in JavaScript. If you append an item to a list, the changes are directly applied to the original list or dictionary. Likewise, in JavaScript, you can change the elements of an array after creation.

The advantages of mutable objects lie in their flexibility and ability to modify data directly, which can be more efficient for certain operations.

The key difference between immutable and mutable objects is that immutable objects cannot be changed once created, while mutable objects allow modifications to their internal state. The choice between using mutable or immutable data types depends on the specific requirements of a program, performance considerations, and the need to ensure data integrity. Immutable objects are often preferred in situations where immutability can simplify the code and avoid potential bugs caused by unexpected changes in data.

To help remember the differences in terminology, think of a mutable object as an object that can have a mutation, i.e., a change.

Latest Articles

Single Responsibility Principle

The Single Responsibility Principle (SRP) is one of the five SOLID principles of object-oriented programming and design, introduced by Robert C...

Read More

Singleton Design Pattern

The Singleton Design Pattern is one of the most commonly used design patterns in software engineering...

Read More

MVC: Model-View-Controller Design Pattern

Model-View-Controller (MVC) is a software architectural pattern used in the design and development of applications, particularly in the context of user interfaces...

Read More

The MEAN Stack

The MEAN stack is a popular technology stack used for building web applications...

Read More

The Internet of Things

The Internet of Things (IoT) refers to the interconnection of everyday objects and devices to the internet, allowing them to collect and exchange data with each other and with centralized systems or applications...

Read More

The Cloud

Cloud computing is a technology that enables users to access and use computing resources (such as servers, storage, databases, networking, software, and more) over the internet and on-demand...

Read More