What is good for using String / StringBuffer and why ?
Please give one example.
Thanks and regards,
s begri wrote:What is good for using String / StringBuffer and why ?
It's a design question. Both represent a sequence of chars. The difference is that String is immutable (cannot change after creation), whereas StringBuffer (and its cousin StringBuilder) are mutable and can be changed.
So you use String when you want to represent a char sequence that won't change, and you use StringBuffer when you have a char sequence you want to manipulate. Think of a String as a char constant and of a StringBuffer as a char collection.
StringBuilder is good for coding.
That question is a bit like asking whether a bicycle is good for going there, or a boat. Both are good, but it depends where you are going. If you want to go across the river and there is no bridge, you need a bicycle. If you want to go up the street to the shops, the boat is better. Or maybe the other way round.
The two classes are completely different. One is for things you want to change, the other for things you want to preserve. The two are closely related. You can put a String into a StringBuilder and change the contents as much as you like. You can get the contents of a StringBuilder into a String where it remains unchangeable.
Don't use StringBuffer; this is less efficient because of the synchronisation, which you don't need in 99% of occasions.
s begri wrote:Which one is good for coding ?
Well, that's what I tried to explain.
If you want a rule of thumb you can always start with a String and then switch to StringBuffer/StringBuilder if you get performance problems. When you use a String in ways that would be faster using a StringBuffer/StringBuilder the compiler actually uses the latter under the hood anyway.
So although the main advice is to use the data structure that best suits the problem, you can always start with a String relying on the compiler to sort out most forms of abuse.
You do not use String or StringBuilder because they have performance differences. You use String because it is immutable and preserves its contents, and you use StringBuilder because it is mutable and allows you to change its contents.
Rule of thumb: if it should change, StringBuilder. If it can be held constant, String. Avoid StringBuffer as obsolete.
Campbell Ritchie wrote:With you about performance. I disagree about "String until you have a performance problem"; the two classes have different functions.
In that case I suggest you read the whole thread. The post you picked on was part of an ongoing exchange with the OP.
Besides you're wrong. Performance is paramount when choosing between different data structures. Performance is the only yardstick by which you can measure whether you made a good choise or not. When to use which is determined only by how well they're expected to perform in the situation at hand. To claim there are other aspects is just balderdash.
So, as a rule of thumb, if you don't know which of String or StringBuilder to use, go for String. If you stretch String a bit and use it more like a dynamic char sequence, rather than the constant char sequence it is, the compiler will step in and optimize to make the String behave like a StringBuilder. This feature is defined by the JLS and should be implemented by all major compilers.
There are things StringBuilders do well; if you want to put lots of bits together with insertions or deletions, use a StringBuilder because that is what it is designed for. There are things StringBuilders do badly, like acting as Keys for Maps or thread-safety. For that sort of thing, you can very easily convert the StringBuilder to a String. There are things Strings do badly, eg insertions, which are very easy if you pass the String to a StringBuilder constructor.
you should not consider speed of performance when choosing a "data structure"; you should consider what you are going to do with it. A List maintains insertion order and a Set automatically deletes duplicates. You choose List or Set depending on whether you want to maintain duplicates, not because one is faster than the other. String and StringBuilder are designed for different purposes; you choose StringBuilder if you want changes, then you change to String when you have finished with the changes.
In a lot of cases, choosing a class for its functionality will give fast performance, but you choose for correct functionality and fast performance is a secondary consideration. The principal yardstick you should measure your code by is correctness. Incorrect code which performs very quickly is of no use, and may in the wrong circumstances be positively dangerous. In the case of List which I mentioned earlier, we have two popular choices (ArrayList and LinkedList), which will both give correct functionality for most uses, but we all know they perform certain operations at different speeds, so there performance will legitimately guide our choice.
String and StringBuilder keep quiet about what data structure they support (but everybody knows it is char), but their functionality handles that char differently.
Campbell Ritchie wrote:Still disagree with you.
Have you read the first post I made in this thread? The one where I mentioned the difference between String and StringBuffer and where I said the choise between the two is a design decision.
Do you know what a rule of thumb is? It's what you fall back on when it's hard to make a fully informed choise, either because you don't have the skills so you need to trust common experience, or you lack adequate information so you rather trust common experience than mere guess.
In his second post, having read my first post I hope, the OP still indicated he wanted something more "hands-on". That's why I suggested a rule of thumb, namely to pick a String over a StringBuffer/StringBuilder when in doubt.
Now what's wrong with this rule of thumb? Show one situation where it would break a design? The Java language has even been adapted to support it (by relaxing performance hits).
I'll be happy to discuss the rest of your views (which I find naive) but first I would like to know what's wrong with the rule of thumb under discussion.
You then produced a rule of thumb (yes, of course I know what a rule of thumb is) and suggested the difference is in terms of performance. But it isn't. It is in what the two are designed for, as you said first. You then suggested that performance is your priority. Your original premise was correct. If you want to change some "text", use StringBuilder, if you want to keep it constant use String.
I agree that in most cases people actually do want String.