Difference between revisions of "Immutable object"
Karl Jones (Talk | contribs) |
Karl Jones (Talk | contribs) |
||
Line 1: | Line 1: | ||
− | In object-oriented and functional programming, an '''immutable object''' ( or '''unchangeable object''') is an object whose [[State (computer science)|state]] cannot be modified after it is created. | + | In object-oriented and functional programming, an '''immutable object''' ( or '''unchangeable object''') is an [[Object (computer science)|object]] whose [[State (computer science)|state]] cannot be modified after it is created. |
This is in contrast to a '''mutable object''' (or '''changeable object'''), which can be modified after it is created. | This is in contrast to a '''mutable object''' (or '''changeable object'''), which can be modified after it is created. | ||
Line 16: | Line 16: | ||
* [[Functional programming]] | * [[Functional programming]] | ||
+ | * [[Object (computer science)]] - a location in memory having a [[Value (computer science)|value]] and possibly referenced by an [[identifier]]. | ||
* [[State (computer science)]] -all the stored information, at a given instant in time, to which a [[logic circuit]] or [[computer program]] has access. | * [[State (computer science)]] -all the stored information, at a given instant in time, to which a [[logic circuit]] or [[computer program]] has access. | ||
* [[String (computer science)]] - a sequence of characters, either as a literal constant or as some kind of variable. | * [[String (computer science)]] - a sequence of characters, either as a literal constant or as some kind of variable. |
Revision as of 15:22, 22 August 2016
In object-oriented and functional programming, an immutable object ( or unchangeable object) is an object whose state cannot be modified after it is created.
This is in contrast to a mutable object (or changeable object), which can be modified after it is created.
Description
In some cases, an object is considered immutable even if some internally used attributes change but the object's state appears to be unchanging from an external point of view. For example, an object that uses memoization to cache the results of expensive computations could still be considered an immutable object.
Strings and other concrete objects are typically expressed as immutable objects to improve readability and runtime efficiency in object-oriented programming.
Immutable objects are also useful because they are inherently thread-safe.
Other benefits are that they are simpler to understand and reason about and offer higher security than mutable objects.
See also
- Functional programming
- Object (computer science) - a location in memory having a value and possibly referenced by an identifier.
- State (computer science) -all the stored information, at a given instant in time, to which a logic circuit or computer program has access.
- String (computer science) - a sequence of characters, either as a literal constant or as some kind of variable.
External links
- Immutable object @ Wikipedia