Can some one pls tell me the exact difference between mutable and immutable ?

Showing Answers 1 - 4 of 4 Answers

Madhu CM

  • Jul 11th, 2006
 

muttable means you can change the value , it is treated as constant... suppose you have String str1 = "java" , then the string "java" is constant. if re-initalize str1 = " kava", the value doesnt change to "kava" instead it remains as "java" only...... for muttable(changeable value) you should use the class called StringBuffer , where you can change the value.... StringBuffer str1 = new StringBuffer("java");System.out.println(str1) // o/p "java" and str1 = "kava" System.out.println(str1) // o/p "kava"

  Was this answer useful?  Yes

Guest

  • Jul 12th, 2006
 

Immutable means unchangeable i.e. U cannot change the content and length of String object. But StringBuffer object is Mutable. i. e. u can change length, content etc.. Let me explain with an example,

    String s1=new String("Senthil");

    String s2=s1.replace('e','a') ;

    In this example, the method replace() return a string "Santhil" but it doesnt change the original string s1. It remains "Senthil".

    System.out.println("Befor Replacing :"+s1+"n Replacing:"+s1.replace('e','a')+"n After Replacing:"+s1)

Befor Replacing :Senthil
 Replacing:Santhil
 After Replacing:Senthil

The above is the output u will have. It clearly say a String is Immutable.

come to StringBuffer,

It is Mutable,

StringBuffer s1=new StringBuffer("Senthil");
     System.out.println("Befor Replacing :"+s1+"n Replacing:"+s1.replace(1,2,"a")+"n After Replacing:"+s1);

Befor Replacing :Senthil
 Replacing:Santhil
 After Replacing:Santhil

the replace() method is differ in String class and StringBuffer class. But only difference is it reflects the change to original String in StringBuffer.

I hope you are clear with Mutable and Immutable...Isn't It.

  Was this answer useful?  Yes

luckshme

  • Jul 31st, 2006
 

A class is said to be designed for Mutable objects if the class provides public attributes that can be directly changed or public methods that can change the values of private attributes thereby changing the value/state of the entire object.

A Classical example of Mutable class is StringBuffer in which you may append, update or delete characters or sub-strings. Most of the classes that programmers code are Mutable.

A class is said to be designed for Immutable objects if the class provides no means to modify the private data directly or indirectly. Yet these classes may provide methods to read the private data.

Classical examples of Immutables are all Wrapper classes like String, Integer, Float etc, java.math.BigDecimal. 

There is no declarative syntax to make a class mutable or immutable in any programming language. It is simply a design pattern.

  Was this answer useful?  Yes

manoj

  • Aug 21st, 2006
 

Scripting languages often follow the syntax and semantics of command languages. For instance, many scripting languages do not require quoting of string literals, but rather require explicit evaluation of variables (x denotes the string "x", but $x denotes the value of the variable x).

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions