SCJP6
When using the "+" sign the compiler automatically create a StringBuilder, does it do the same if you write a multi-thread application? What is the bad things that can happen if this case?
The only difference between StringBuffer and StringBuilder is that StringBuilder is unsynchronized whereas StringBuffer is synchronized. So when the application needs to be run only in a single thread then it is better to use StringBuilder. StringBuilder is more efficient than StringBuffer.
be a well encapsulated person, don't expose your privates, unless you public void getWife()!
SCJP6, SCWCD5, OCE:EJBD6.
BLOG: http://leakfromjavaheap.blogspot.com
Lukas Smith wrote:So why does not "+ operator" invoke concat() method?
Why does it create StringBuilder?
[SCJP 6.0]
Lukas Smith wrote:So why does not "+ operator" invoke concat() method?
Why does it create StringBuilder?
Henry Wong wrote:
Lukas Smith wrote:So why does not "+ operator" invoke concat() method?
Why does it create StringBuilder?
Generally, "why" questions require the Java designers to answer, as we don't know what they were thinking... but we can speculate. Possible answers are....
1. StringBuilder can chain. For example...
becomes...
Just create one string builder to create the resultant string.
2. StringBuilder can be used to handle non string types. For example...
becomes...
The string concat() method only does the concatenation of strings -- and then, only two at a time.
Henry
SCJP6, SCWCD5, OCE:EJBD6.
BLOG: http://leakfromjavaheap.blogspot.com
Lukas Smith wrote:
OK, thanks for reply, but concat(...) can be also chained but on the other hand it can take only String.
SCJP6, SCWCD5, OCE:EJBD6.
BLOG: http://leakfromjavaheap.blogspot.com