• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Mutable

 
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is mutable and immutable? why string objects are called immutable?
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Refering to www.dictionary.com,

Mutable means - That which is capable or subject to change or alteration.

A String in Java is atomic It cannot be changed. Once a String object is created on the heap, the literals (alphabets of the String) cannot be changed.

String s = "Sun";

String class does not have any methods to modify "Sun" to "Son"

To modify a String , you have to push the contents to a StringBuffer and then modify. The resultant is a NEW STRING object. However the source string remains untouched.

Thanks
 
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Immutable means " Once you have assigned a String a value, that value can never change "— it's immutable,
frozen solid.while the String object is immutable, its reference variable is not means it can refer to another String object if it's diected to refer another String object leaving previous String object unchanged.

String s = "abcdef"; // create a new String object, with
// value "abcdef", refer s to it

String s2 = s; // create a 2nd reference variable
// referring to the same String

// create a new String object, with value "abcdef more stuff",
// refer s to it. (Change s's reference from the old String
// to the new String.) ( Remember s2 is still referring to
// the original "abcdef" String.)

s = s.concat(" more stuff");

The main advantage of String object's immutability is that there may be many references to single String object
and if any reference changes the String object it'll reflect to all other references,which is not a good idea so in case any referense changes contents of String object a new String object is created and reference is directed to newly created String.

i hope this will help you..

Regards
 
This cake looks terrible, but it tastes great! Now take a bite out of this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic