String And StringBuffer

Discussions

General J2EE: String And StringBuffer

  1. String And StringBuffer (2 messages)

    Is there any advantage of defining public static final String over simple Stringbuffers?

    Thanks
    ~Sabya

    Threaded Messages (2)

  2. String And StringBuffer[ Go to top ]

    Less memory used in the stack for the String. Wouldn't have to use the toString method when you wanted to access the values.
  3. String And StringBuffer[ Go to top ]

    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