Wednesday 8 January 2014

Mutable vs Immutable in Java


Mutable Objects: When you have a reference to an instance of an object, the contents of that instance can be altered

Immutable Objects: When you have a reference to an instance of an object, the contents of that instance cannot be altered

(OR)
Immutable objects are simply objects whose state (the object's data) cannot change after construction

Immutability and Instances

To demonstrate this behaviour, we'll use java.lang.String as the immutable class and java.awt.Point as the mutable class.
package raja.JavaPages4all;

import java.awt.Point;

public class Immutability  
{
 /**
  * @author Raja(JavaPages4All)
  * @param args
  */
 public static void main(String[] args) 
 {
  Point pt= new Point( 0, 0 );
  System.out.println( pt);
  pt.setLocation( 1.0, 0.0 );
  System.out.println( pt);

  String str= new String( "old String" );
  System.out.println( str);
  str.replaceAll( "old", "new" );
  System.out.println( str);
 }
}
In case you can't see what the output is, here it is:
 java.awt.Point[0.0, 0.0]
 java.awt.Point[1.0, 0.0]
 old String
 old String

We are only looking at a single instance of each object, but we can see that the contents of pt has changed, but the contents of str did not. To show what happens when we try to change the value of str, we'll extend the previous example.
package raja.JavaPages4all;

import java.awt.Point;

public class Immutability  
{
 /**
  * @author Raja(JavaPages4All)
  * @param args
  */
 public static void main(String[] args) 
 {
  Point pt= new Point( 0, 0 );
  System.out.println( pt);
  pt.setLocation( 1.0, 0.0 );
  System.out.println( pt);

  String str= new String( "old String" );
  System.out.println( str );
  str= new String( "new String" );
  System.out.println( str );
 }
}
The output from this is:
 old String
 new String
Now we find that the value displayed by the str variable has changed. We have defined immutable objects as being unable to change in value, so what is happening? Let's extend the example again to watch the str variable closer.
package raja.JavaPages4all;


public class Immutability  
{
 /**
  * @author Raja(JavaPages4All)
  * @param args
  */
 public static void main(String[] args) 
 {
  //Comparison
  String str = new String( "old String" );
  String myCache = str ;
  System.out.println( "String Comparison: " + str.equals( myCache ) );
  System.out.println( "Object Comparison:  " + ( str == myCache ) );

  str = "not " + str ;
  System.out.println( "String Comparison: " + str.equals( myCache ) );
  System.out.println( "Object Comparison:  " + ( str == myCache ) );
 }
}
The result from executing this is:
 String Comparison: true
 Object Comparison: true
 String Comparison: false
 Object Comparison: false
What this shows is that variable str is referencing a new instance of the String class. The contents of the object didn't change; we discarded the instance and changed our reference to a new one with new contents.


No comments:

Post a Comment