An example of immutable is something that has happened and cannot be reversed. Unable to be changed without exception. The government has enacted an immutable law. (programming, of a variable) Not able to be altered in the memory after its value is set initially, such as a constant.
If the value can change, the object is called mutable, while if the value cannot change, the object is called immutable.
A stateless object saves no application data; it is idempotent. Each operation on the object is independent of any other operations.
To summarise the difference, mutable objects can change their state or contents and immutable objects can't change their state or content. Immutable Objects : These are of in-built types like int, float, bool, string, unicode, tuple. In simple words, an immutable object can't be changed after it is created.
I often hear the term "Stateless" and "Immutable". For example, HTTP is a stateless protocol, and a String object is an immutable object. But I have a hard time grasping the difference between the two. When I create a stateless object, it doesn't store any "state" data internally. And if I create an Immutable object, it mean its will never change.
Doesn't that mean the same thing?
Since the immutable object doesn't change, by definition it cannot have a state. It is what it is forever. And if an object doesn't have state, it cannot be mutated (by definition). Thus, aren't all stateless object immutable and immutable object stateless?
What could be an example of mutable stateless object or immutable stateful object?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With