• 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
  • Ron McLeod
  • Tim Cooke
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Jeanne Boyarsky
Saloon Keepers:
  • Stephan van Hulst
  • Carey Brown
  • Tim Holloway
  • Piet Souris
Bartenders:

String

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I want to know know that what is the basic concept of String and StringBuffer?
Why string is called immutable and String Buffer called mutable?
 
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Neelam,
Immutable means that it can't be changed once created. That means that if we have the following:
String s1 = new String( "First" );
The string object "First" can never be changed. I can however change the reference object s1 to point to another string.
s1 += " One";
The object s1 will now point to a string: "First One".
The string "First" will probably be garbage collected by JVM because no references to it exist!
On the other hand, using StringBuffer we can actually change the object contents that the stringbuffer references.
StringBuffer sb = new StringBuffer("First");
sb.append(" One");
Now we no longer have a stringbuffer containing "First" we only have a stringbuffer "First One". In other words, we have changed the string buffer contents.
Immutable --> contents can not be changed
Mutable --> contents can be changed
Regards,
Manfred.
 
neelam samnani
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Manfred Leonhardt:
Hi Neelam,
Immutable means that it can't be changed once created. That means that if we have the following:
String s1 = new String( "First" );
The string object "First" can never be changed. I can however change the reference object s1 to point to another string.
s1 += " One";
The object s1 will now point to a string: "First One".
The string "First" will probably be garbage collected by JVM because no references to it exist!
On the other hand, using StringBuffer we can actually change the object contents that the stringbuffer references.
StringBuffer sb = new StringBuffer("First");
sb.append(" One");
Now we no longer have a stringbuffer containing "First" we only have a stringbuffer "First One". In other words, we have changed the string buffer contents.
Immutable --> contents can not be changed
Mutable --> contents can be changed
Regards,
Manfred.



Hi,
Sir, Actually , I want to what is there in Java or JVM to make string immutable and stringbuffer mutable? Can you help me?
I 'll very greatfull to You.
 
He loves you so much! And I'm baking the cake! I'm going to put this tiny ad in the cake:
The Low Tech Laboratory Movie Kickstarter is LIVE NOW!
https://www.kickstarter.com/projects/paulwheaton/low-tech
reply
    Bookmark Topic Watch Topic
  • New Topic