Difference between Mutable objects and Immutable objects

JavaTerminology

Java Problem Overview


Any one please give the diff between Mutable objects and Immutable objects with example.

Java Solutions


Solution 1 - Java

Mutable objects have fields that can be changed, immutable objects have no fields that can be changed after the object is created.

A very simple immutable object is a object without any field. (For example a simple Comparator Implementation).

class Mutable{
  private int value;

  public Mutable(int value) {
     this.value = value;
  }

  //getter and setter for value
}

class Immutable {
  private final int value;

  public Immutable(int value) {
     this.value = value;
  }

  //only getter
}

Solution 2 - Java

Mutable objects can have their fields changed after construction. Immutable objects cannot.

public class MutableClass {

 private int value;
 
 public MutableClass(int aValue) {
  value = aValue;
 }
 
 public void setValue(int aValue) {
  value = aValue;
 }
 
 public getValue() {
  return value;
 }
 
}

public class ImmutableClass {

 private final int value;
 // changed the constructor to say Immutable instead of mutable
 public ImmutableClass (final int aValue) {
  //The value is set. Now, and forever.
  value = aValue;
 }
 
 public final getValue() {
  return value;
 }
 
}

Solution 3 - Java

Immutable objects are simply objects whose state (the object's data) cannot change after construction. Examples of immutable objects from the JDK include String and Integer.

For example:(Point is mutable and string immutable)

     Point myPoint = new Point( 0, 0 );
	System.out.println( myPoint );
	myPoint.setLocation( 1.0, 0.0 );
	System.out.println( myPoint );

	String myString = new String( "old String" );
	System.out.println( myString );
	myString.replaceAll( "old", "new" );
	System.out.println( myString );

The output is:

java.awt.Point[0.0, 0.0]
java.awt.Point[1.0, 0.0]
old String
old String

Solution 4 - Java

Immutable Object's state cannot be altered.

for example String.

String str= "abc";//a object of string is created
str  = str + "def";// a new object of string is created and assigned to str

Solution 5 - Java

They are not different from the point of view of JVM. Immutable objects don't have methods that can change the instance variables. And the instance variables are private; therefore you can't change it after you create it. A famous example would be String. You don't have methods like setString, or setCharAt. And s1 = s1 + "w" will create a new string, with the original one abandoned. That's my understanding.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRamsView Question on Stackoverflow
Solution 1 - JavaRalphView Answer on Stackoverflow
Solution 2 - JavaMikeView Answer on Stackoverflow
Solution 3 - Javauser536158View Answer on Stackoverflow
Solution 4 - JavajmjView Answer on Stackoverflow
Solution 5 - JavaLLSView Answer on Stackoverflow