Site banner
.
Home Forums Blogs Articles Photos Videos Contact FAQ                    
.
.
Wisdom Archive
Body Mind and Soul
Faith and Belief
God and Religion
Law of Attraction
Life and Beyond
Love and Happiness
Peace of Mind
Peace on Earth
Personal Faith
Spiritual Festivals
Spiritual Growth
Spiritual Guidance
Spiritual Inspiration
Spirituality and Science
Spiritual Retreats
More Wisdom
Buddhism Archives
Hinduism Archives
Sustainability
Theology Archives
Even more Wisdom
2012 - Year 2012
Affirmations
Aura
Ayurveda
Chakras
Consciousness
Cultural Creatives
Diksha (Deeksha)
Dream Dictionary
Dream Interpretation
Dream interpreter
Dreams
Enlightenment
Essential Oils
Feng Shui
Flower Essences
Gaia Hypothesis
Indigo Children
Kalki Bhagavan
Karma
Kundalini
Kundalini Yoga
Life after death
Mayan Calendar
Meaning of Dreams
Meditation
Morphogenetic Fields
Psychic Ability
Reincarnation
Spiritual Art, Music & Dance
Spiritual Awakening
Spiritual Enlightenment
Spiritual Healing
Spirituality and Health
Spiritual Jokes
Spiritual Parenting
Vastu Shastra
Womens Spirituality
Yoga Positions
Site map 2
Site map


Dream Sharing Forum

at Global Oneness Community.

Share your dreams and let others help you with the interpretation!
Dream Sharing Forum



.

Immutable object

Immutable object: Encyclopedia - Immutable object

In computer science, an immutable object, as opposed to a mutable object, is a kind of object whose internal state cannot be modified after it is created. An object can be either entirely immutable or some attributes in the object may be declared immutable, as in C++'s const member data attribute. 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 ...

Including:

Immutable object, Immutable object - Background, Immutable object - Example, Immutable object - Implementation, Immutable object - Usage

Immutable object: Encyclopedia - Immutable object



Immutable object

In computer science, an immutable object, as opposed to a mutable object, is a kind of object whose internal state cannot be modified after it is created. An object can be either entirely immutable or some attributes in the object may be declared immutable, as in C++'s const member data attribute. 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. The initial state of an immutable object is usually set at its inception, but can also be set before actual use of the object.

Immutable objects are often useful because some costly operations for copying and comparing can be omitted, simplifying the program code and speeding execution. However, making an object immutable is usually inappropriate if the object contains a large amount of changable data. Because of this, many languages allow for both immutable and mutable objects.

Immutable object - Background

In most object-oriented languages, objects can be referred to using references. Some examples of such languages are Java, C++, and many scripting languages, such as Python and Ruby. In this case, it matters whether the state of object can vary when objects are shared via references.

If an object is known to be immutable, it can be copied simply by making a copy of a reference to it instead of copying the entire object. Because a reference (typically only the size of a pointer) is usually much smaller than the object itself, this results in memory savings and a boost in execution speed.

The reference copying technique is much more difficult to use for mutable objects, because if any user of a reference to a mutable object changes it, all other users of that reference will see the change. If this is not the intended effect, it can be difficult to notify the other users to have them respond correctly. In these situations, defensive copying of the entire object rather than the reference is usually an easy but costly solution. The observer pattern is an alternative technique for handling changes to mutable objects.

Immutable objects can be useful in multi-threaded applications. Multiple threads can act on data represented by immutable objects without concern of the data being changed by other threads. Immutable objects are therefore considered to be more thread-safe than mutable objects.

The practice of always using references in place of copies of equal objects is known as interning. If interning is used, two objects are considered equal if and only if their references, typically represented as integers, are equal. Some languages do this automatically: for example, Python automatically interns strings. If the algorithm which implements interning is guaranteed to do so in every case that it is possible, then comparing objects for equality is reduced to comparing their pointers, a substantial gain in speed in most applications. (Even if the algorithm is not guaranteed to be comprehensive, there still exists the possibility of a fast path case improvement when the objects are equal and use the same reference.) Interning is generally only useful for immutable objects.

Immutable object - Implementation

Immutability does not imply that the object as stored in the computer's memory is unwriteable. Rather, immutability is a compile-time construct that indicates what a programmer should do, not necessarily what she can do (for instance, by circumventing the type system or violating const correctness in C or C++).

A technique which blends the advantages of mutable and immutable objects, and is supported directly in almost all modern hardware, is copy-on-write (COW). Using this technique, when a user asks the system to copy an object, it will instead merely create a new reference which still points to the same object. As soon as a user modifies the object through a particular reference, the system makes a real copy and sets the reference to refer to the new copy. The other users are unaffected, because they still refer to the original object. Therefore, under COW, all users appear to have a mutable version of their objects, although in the case that users do not modify their objects, the space-saving and speed advantages of immutable objects are preserved. Copy-on-write is popular in virtual memory systems because it allows them to save memory space in the core while still correctly handling anything an application program might do.

Immutable object - Example

A classic example of an immutable object is an instance of the Java String class.

String s = "ABC";
s.toLower();

The method toLower() will not change the data "ABC" that s contains. Instead, a new String object is instantiated and given the data "abc" during its construction. A reference to this String object is returned by the toLower() method. To make the String s contain the data "abc", a different approach is needed.

s = s.toLower();

Now the String s references a new String object that contains "abc". The String class's methods never affect the data that a String object contains.

For an object to be immutable, there has to be no way to change fields, mutable or not, and to access fields that are mutable. Here is an example of a mutable object written in the Java programming language.

class Cart {
  private final List items;
   
  public Cart(List items) { this.items = items; }
   
  public List getItems() { return items; }
  public int total() { /* return sum of the prices */ }
}

An instance of this class is not immutable: one can add or remove items either by obtaining the field items by calling getItems() or by retaining a reference to the List object passed when an object of this class is created. The following change partially solves this problem. In the ImmutableCart class, the list is immutable: you cannot add or remove items. However, there is no guarantee that the items are also immutable. One solution is to use the decorator pattern as a wrapper around each of the list's items to make them also immutable.

class ImmutableCart {
  private final List items;
   
  public ImmutableCart(List items) {
    this.items = Arrays.asList(items.toArray());
  }
   
  public List getItems() {
    return Collections.unmodifiableList(items);
  }
  public int total() { /* return sum of the prices */ }
}

In C++, a const-correct implementation of Cart would allow the user to declare new instances of the class as either const (immutable) or mutable, as desired, by providing two different versions of the getItems() method. (Notice that in C++, which lacks automatic garbage collection, it is idiomatic to make a copy of the constructor parameter v rather than keeping only a reference to it, as in the Java code above. Therefore, the C++ code needs only to const-qualify the result of getItems(); it is not necessary — and in fact impossible — to provide a specialized constructor for const instances.)

template<typename T>
class Cart {
 private:
  std::vector<T> items;
   
 public:
  Cart(std::vector<T> v): items(v) { }
   
  std::vector<T>& getItems() { return items; }
  const std::vector<T>& getItems() const { return items; }
  int total() const { /* return sum of the prices */ }
};

Immutable object - Usage

Strings and other concrete objects are typically expressed as immutable objects to improve readability and runtime efficiency in object-oriented programming. In Python and Java, strings are immutable objects. Java also has a class StringBuffer, which is mutable version of the immutable java.lang.String class.

Other examples of immutable classes in Java are Byte, Character, Short, Integer, Long, Float and Double.

Enforcement of the pattern can be checked by using specialized compilers (see for example [1]), and there is a proposal to add immutable types to Java.

Similar patterns are the Immutable Interface and Immutable Wrapper.




Adapted from the Wikipedia article "Immutable object", under the G.N U Free Docmentation License. Please also see http://en.wikipedia.org/wiki

More material related to Immutable Object can be found here:
Main Page
for
Immutable Object
Index of Articles
related to
Immutable Object


« Back








Search the Global Oneness web site
Global Oneness is a huge, really huge, web site. Almost whatever you are searching for within health, spirituality, personal development and inspirationals - you will find it here!
Google
 
 

Rate this article!

Please rate this article with 10 as very good and 1 as very poor.

.








Sneak-Peek of Global Oneness Community

Hi friend! The Global Oneness Community, the place for information and sharing about Oneness is not really launched yet (you will see there is still some clean up to do) ...but it is now open for a sneak-peek! And if you wish - please register and become one of the very first members to do so! Jonas

Forum Home, Articles, Photo Gallery, Videos, News, Sitemap
...and much more!


Dream Sharing Forum

at Global Oneness Community.

Share your dreams and let others help you with the interpretation!
Dream Sharing Forum



Forum
Articles
Images Pictures
Videos
News
Sitemap




 

 

 

 

 


 








  » Home » » Home »