Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Strings are immutable - that means I should never use += and only StringBuffer?

Strings are immutable, meaning, once they have been created they cannot be changed.

So, does this mean that it would take more memory if you append things with += than if you created a StringBuffer and appended text to that?

If you use +=, you would create a new 'object' each time that has to be saved in the memory, wouldn't you?

like image 479
corgrath Avatar asked Oct 28 '09 07:10

corgrath


People also ask

What does it mean if strings are immutable?

The String is immutable, so its value cannot be changed. If the String doesn't remain immutable, any hacker can cause a security issue in the application by changing the reference value. The String is safe for multithreading because of its immutableness. Different threads can access a single “String instance”.

Why is StringBuffer not immutable?

String is immutable, if you try to alter their values, another object gets created, whereas StringBuffer and StringBuilder are mutable so they can change their values. Thread-Safety Difference: The difference between StringBuffer and StringBuilder is that StringBuffer is thread-safe.

Are StringBuffer objects immutable?

The StringBuffer class in java is same as String class except it is mutable i.e. it can be changed.

Why String object is immutable and StringBuffer is mutable?

String is an immutable class and its object can't be modified after it is created but definitely reference other objects. They are very useful in multithreading environment because multiple threads can't change the state of the object so immutable objects are thread safe.


2 Answers

Yes, you will create a new object each time with +=. That doesn't mean it's always the wrong thing to do, however. It depends whether you want that value as a string, or whether you're just going to use it to build the string up further.

If you actually want the result of x + y as a string, then you might as well just use string concatenation. However, if you're really going to (say) loop round and append another string, and another, etc - only needing the result as a string at the very end, then StringBuffer/StringBuilder are the way to go. Indeed, looping is really where StringBuilder pays off over string concatenation - the performance difference for 5 or even 10 direct concatenations is going to be quite small, but for thousands it becomes a lot worse - basically because you get O(N2) complexity with concatenation vs O(N) complexity with StringBuilder.

In Java 5 and above, you should basically use StringBuilder - it's unsynchronized, but that's almost always okay; it's very rare to want to share one between threads.

I have an article on all of this which you might find useful.

like image 60
Jon Skeet Avatar answered Sep 29 '22 01:09

Jon Skeet


Rule of thumb is simple:

If you are running concatenations in a loop, don't use +=

If you are not running concatenations in a loop, using += simply does not matter. (Unless a performance critical application

like image 28
Kyle Rosendo Avatar answered Sep 29 '22 01:09

Kyle Rosendo