Logo 
Search:

Java Answers

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds
  Question Asked By: Lamberta Schmidt   on Jun 22 In Java Category.

  
Question Answered By: Angel Harris   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.

Share: 

 

This Question has 1 more answer(s). View Complete Question Thread

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


Tagged: