Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

difference between String and StringBuffer Objects

  Asked By: Lamberta    Date: Jun 22    Category: Java    Views: 841
  

I needed one information regarding the exact difference
between String and StringBuffer Objects in Java and their maximum memory size.
And also how u will call EJBs From Servlets.

Share: 

 

2 Answers Found

 
Answer #1    Answered By: Kelly Bell     Answered On: Jun 22

A String object can not be changed (it is immutable). A StringBuffer has
different methods to change it.

 
Answer #2    Answered By: Angel Harris     Answered On: Jun 22

While Mark's explanation is strictly correct, it may tend to confuse a bit.

Strings are the nice easy way to do what you want:

- Assign text to string  variables
- Concatenate strings together to create other variables
- Find information  in strings
- Replace parts of strings (creating new string variables)
- etc

Conversely, the text inside StringBuffers can be manipulated directly - i.e. you
can change the text in-place rather than needing to assign to new strings all
the time. This is slightly more cumbersome to do (and definitely more "old
fashioned") than just working with Strings, but some people prefer it. It is
probably slightly more efficient in terms of CPU cycles, but this is hardly ever
important.

Internally, the compiler will sometimes convert Strings to and from
StringBuffers to do manipulations, but this is transparent to you.

I would always recommend using Strings except where (1) the code to do something
using a StringBuffer is actually easier to read / maintain than the equivalent
code using Strings or (2) speed is an important factor.

Programmers who work with multiple languages (and that's pretty much all of us,
because we're using at least JavaScript as well) favour as consistent an
approach as possible to string manipulation. StringBuffers are peculiar to
Java.

 
Didn't find what you were looking for? Find more on difference between String and StringBuffer Objects Or get search suggestion and latest updates.