Mutability &
Immutability
.
Lecture 15
Mutable objects have fields that can be
modified, the immutable objects have no
fields that can be changed after the object is
created. Immutable objects are objects
whose state can not be changed after they
have been created.
In Java, everything (except for strings) is
mutable by default.
There's no way to
make existing
objects immutable.
Even if an object is
declared final, its
fields can still be
changed. Let’s see
this in the next slide.
Here, although we
have made object ‘p’
final, but still we
can change the
field.
Note: In Java, the final
keyword can be used
while declaring an entity.
Using the final keyword
means that the value can't
be modified in the future.
So what do we do now? We can make its objects immutable by making
all fields final and private. Now we cannot modify the value of x or y
after the object has been created. The object has become
immutable means state cannot be changed after its creation.
String is an example of an immutable type. A String object
always represents the same string.
Since String is immutable, once created, a String object
always has the same value. To add something to the end of a
String, you have to create a new String object:
StringBuilder is an example of a mutable type. It has
methods to delete parts of the string, insert or replace
characters, etc. Means methods that change the value of the
object.
In both cases, you end up with s and sb referring to the
string of characters "ab" . The difference between
mutability and immutability doesn’t matter much when
there’s only one reference to the object. But there are
big differences in how they behave when there are
other references to the object.
For example, when another variable t points to the same
String object as s , and another variable tb points to the
same StringBuilder as sb , then the differences between the
immutable and mutable objects become more evident:

Lec15_Mutability Immutability-converted.pptx

  • 1.
  • 2.
    Mutable objects havefields that can be modified, the immutable objects have no fields that can be changed after the object is created. Immutable objects are objects whose state can not be changed after they have been created. In Java, everything (except for strings) is mutable by default.
  • 3.
    There's no wayto make existing objects immutable. Even if an object is declared final, its fields can still be changed. Let’s see this in the next slide.
  • 4.
    Here, although we havemade object ‘p’ final, but still we can change the field. Note: In Java, the final keyword can be used while declaring an entity. Using the final keyword means that the value can't be modified in the future.
  • 5.
    So what dowe do now? We can make its objects immutable by making all fields final and private. Now we cannot modify the value of x or y after the object has been created. The object has become immutable means state cannot be changed after its creation.
  • 6.
    String is anexample of an immutable type. A String object always represents the same string. Since String is immutable, once created, a String object always has the same value. To add something to the end of a String, you have to create a new String object:
  • 7.
    StringBuilder is anexample of a mutable type. It has methods to delete parts of the string, insert or replace characters, etc. Means methods that change the value of the object. In both cases, you end up with s and sb referring to the string of characters "ab" . The difference between mutability and immutability doesn’t matter much when there’s only one reference to the object. But there are big differences in how they behave when there are other references to the object.
  • 8.
    For example, whenanother variable t points to the same String object as s , and another variable tb points to the same StringBuilder as sb , then the differences between the immutable and mutable objects become more evident: