Reusing a StringBuilder is more efficient CPU wise. However, I doubt it will really matter. You should do what you consider the most natural and clearest approach is.(Which appears to be StringBuilder from what you are saying) Performance alone is less often a good reason to do something than many people think.
Agreed about the CPU side from an Object creation standpoint. But the CPU usage on garbage collection can be a big deal if you are doing a LOT of unnecessary and/or wasteful string concatenation. From that standpoint, I would prefer being smarter with StringBuilder and/or StringBuffer.
– Chris Aldrich Sep 16 at 15:49 I like writing gc-less code myself. I would use a ThreadLocal instead and create no objects, even the final String (which contains a char). But its not for everyone.
;) – Peter Lawrey Sep 16 at 15:57 @Peter, keep in mind if code like above is used insde of a web page, which is accessed by millions of users simultaniously, performance counts a lot. It is a really really bad habit to use "+" if it is not a toy program. – Angel O'Sphere Sep 16 at 15:58 Using + often is bad.
Using it occasionally may not be a problem. If it were only used in buildMessage(), it might be twice. Using it in the building the body is more likely to be a bit of a disaster as it could be called rather a lot.
– Peter Lawrey Sep 16 at 16:01.
Basically, with the first option, you are suffering from two distinct inefficiencies. 1). You are creating a stringBuilder object 3 additional times (so a total of 4 times), whereas in the second option it is only created once and reused.
The creation of the object must be worth it, so usually you use stringBuilder when you are doing multiple string manipulation tasks on a string. 2). Because each header, body and footer return a string, in each case you are creating a normal string and copying it unnecessarily, this is defeating the purpose of using stringBuilder.
Bascially, concating a normal string just recreates it from scratch, so you are suffering the same overheads that concating incurs when you create (as concating is just recreating from scratch as well). You are probably better off creating a class, and in the constructor of the class you can create your string builder object so you don't need to do this in your upper level code. Inside the class you can have the three methods, each one of them using the private string builder object when it builds and ultimately returns string.
This way you can just make your calls without passing a string builder argument and without creating the object in your upper level code (the messy stuff is taken care of in the class automatically as per your code, this will make it easier to read)... :) I hope this helps. If you would like me to explain further, do let me know. Also, you should only worry about these things if there are a large number of calls to these procedures on your server, and especially if this is happening in a For loop, and even more so if both these points are true or apply :).
Cheers mate.
Just because I don't see it mentioned clearly in the other answers: private String getHeader() { StringBuilder builder = new StringBuilder(); builder. Append("append(this. Firstname) .
Append(",\n"); return builder.toString(); } can be replaced with private StringBuilder builder = new StringBuilder(); private String getHeader() { builder. SetLength(0); builder. Append("append(this.
Firstname) . Append(",\n"); return builder.toString(); } The buffer will be allocated once. When the buffer is extended, the extension is kept until the declaring object is garbage collected.So, no time lost in re-allocation and resizing of the buffer.
Only glitch is that you may need to make your method synchronous (i.e. Thread safe). Using an existing string buffer is faster than a string, since no allocation is required.
The manual concatenation of strings is quite expensive, since multiple allocations are (often) required. P.S.: Of course, the private allocation of the string builder can be shared between private methods, meaning the end user does not need to allocate it itself.In some cases, passing the buffer makes the code easier to read and more functional. There is no definite answer or rule here, it really depends of what you are doing and what you need.
If you are multi-threaded then either declare a ThreadLocal instance of StringBuilder, or use StringBuffer as it works better with concurrent access. – Chris Aldrich Sep 16 at 16:28 @Chris That's where having the user provide the string buffer is a better option, since no synchronization is required then. – JVerstry Sep 16 at 17:33 The code you wrote is exactly what I described, the only diff is you didn't wrap a class around it.Cheers.
– Erx_VB.NExT. Coder Sep 16 at 18:07 ...and the resizing/re-allocation motivation + necessity to set length to 0 + thread-safety discussion + readability discussion. – JVerstry Sep 16 at 18:18 @jverstry I was just referring to your code, not the discussions part, I liked the discussion topics you made and up voted you for that reason.
– Erx_VB.NExT. Coder Sep 167 at 18:34.
Wishful thinking This would be great - if StringBuilder wasn't a final class. Put the append... methods in an inner class that extends StringBuilder: public class MessageBuilder { public String buildMessage() { return "" + MyStringBuilder() .appendHeader() .appendBody() .appendFooter(); } private class MyBuilder extends StringBuilder { MyBuilder appendHeader() { append("append(this. Firstname) .
Append(",\n"); } } An approach that actually works - with a lot more work - is to write a (non-final! ) delegate class for StringBuilder, and implement private special-purpose extensions like this one as subclasses of it. See discussion here.
Mostly CPU utilization is the issue. But if you have a newer PC with Processing power it will be irrelevant. I agree with Peter, and think you should use StringBuilder, especially when the strings get larger and harder to parse/manage.
The last approach would be a true builder pattern if the "StringBuilder" would be hidden in an instance variable. The reason to use that design pattern (the first examples are NOT the designpattern Builder) is to be able to make derived classes that overwrite the appendHeader / appendFooter and appendBody methods (e.g. You would want to build small HTML snippet. If you only want to elegantly construct a string, then the fluent example (the second last one) looks the best, and is likely for younger programmers most easiest to understand.
Ah, some clueless gave me -1 without even commenting. Nice. – Angel O'Sphere Sep 27 at 12:03.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.