انت هنا الان : شبكة جامعة بابل > موقع الكلية > نظام التعليم الالكتروني > مشاهدة المحاضرة

Mutable and Immutable Objects

الكلية كلية تكنولوجيا المعلومات     القسم قسم شبكات المعلومات     المرحلة 3
أستاذ المادة مهدي عبادي مانع الموسوي       28/10/2013 09:56:45
1.1. Mutability

All objects either are immutable or mutable . the state of an immutable object never changes, while the state of a mutable object can change.
String object are mutable , for example String methods have a concatenation operator +, but is does not modify either of its argument, instead, it returns a new string whose state is the concatenation of the states of its arguments. If we did the following assignment to t with the state shown in Figure (b).

t=t + "g";

the results as shown in figure (c) is that t now refers to an a new String object whose state is "abcdefg" and the object referred to by s is unaffected.

On the other hand, arrays are mutable. The assignment
a[i]=e

causes the state of array a to change by replacing its ith element with the value obtained by evaluating expression e(the modification occurs only if i is in bounds for a, an exception is thrown).

Discussion Note
If a mutable object is shared by two or more variables, modifications made through one of the variables will be visible when the object is used through the other variable.

Example
Suppose the shared array in figure (b) is modified by
b[0]=i; // i= 6 before this causes the zeroth element of the array to contain 6 ( instead of 1 it used to contain ).


المادة المعروضة اعلاه هي مدخل الى المحاضرة المرفوعة بواسطة استاذ(ة) المادة . وقد تبدو لك غير متكاملة . حيث يضع استاذ المادة في بعض الاحيان فقط الجزء الاول من المحاضرة من اجل الاطلاع على ما ستقوم بتحميله لاحقا . في نظام التعليم الالكتروني نوفر هذه الخدمة لكي نبقيك على اطلاع حول محتوى الملف الذي ستقوم بتحميله .