Win a copy of Terraform in Action this week in the Cloud forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic