Is there any advantage of defining public static final String over simple Stringbuffers?
Thanks
~Sabya
-
String And StringBuffer (2 messages)
- Posted by: Sabyasachi Chowdhury
- Posted on: August 09 2005 18:06 EDT
Threaded Messages (2)
- String And StringBuffer by Rich Hill on August 10 2005 09:00 EDT
- String And StringBuffer by abhijeet mahalkar on September 27 2005 04:21 EDT
-
String And StringBuffer[ Go to top ]
- Posted by: Rich Hill
- Posted on: August 10 2005 09:00 EDT
- in response to Sabyasachi Chowdhury
Less memory used in the stack for the String. Wouldn't have to use the toString method when you wanted to access the values. -
String And StringBuffer[ Go to top ]
- Posted by: abhijeet mahalkar
- Posted on: September 27 2005 04:21 EDT
- in response to Sabyasachi Chowdhury
JVM uses a uses a pool in a memory for strings. If you have string of same value then you will have only one object of that value will be present in the memory an both the reference objects will be poiting to the same object in the pool. That's the reason Strings are immutable. if you change the value of string object, JVM basically create a new string object in the pool and the reference of this object will be stored in the given reference pointer. the old string object will be collected by Garbage collector if you don't have any reference object pointing to it.
StringBuffers are normal java objects. When you call replace() on it it actually changes the value of the same object where as this does not happen in the String mechanism.
Hope this will clarify the Difference between string and StringBuffer.
regards
Abhijeet Mahalkar