aspose file tools*
The moose likes Mock Exam Errata and the fly likes String Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Mock Exam Errata
Bookmark "String" Watch "String" New topic
Author

String

neelam samnani
Greenhorn

Joined: Mar 12, 2001
Posts: 8
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?

Neelam
Manfred Leonhardt
Ranch Hand

Joined: Jan 09, 2001
Posts: 1492
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

Joined: Mar 12, 2001
Posts: 8
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: String