<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: Java Performance with Strings and StringBuilder</title>
	<atom:link href="http://www.vogella.de/blog/2009/07/19/java-string-performanc/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.vogella.de/blog/2009/07/19/java-string-performanc/</link>
	<description>Tips around Eclipse and Android programming</description>
	<lastBuildDate>Thu, 09 Feb 2012 16:32:20 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
	<item>
		<title>By: admin</title>
		<link>http://www.vogella.de/blog/2009/07/19/java-string-performanc/comment-page-1/#comment-222</link>
		<dc:creator>admin</dc:creator>
		<pubDate>Sun, 26 Jul 2009 20:56:09 +0000</pubDate>
		<guid isPermaLink="false">http://www.vogella.de/blog/?p=585#comment-222</guid>
		<description>@Benjamin: Thanks for mentioning this! Eclipse can try a concatenated string into a StringBuilder via Ctrl+1.</description>
		<content:encoded><![CDATA[<p>@Benjamin: Thanks for mentioning this! Eclipse can try a concatenated string into a StringBuilder via Ctrl+1.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Benjamin Muskalla</title>
		<link>http://www.vogella.de/blog/2009/07/19/java-string-performanc/comment-page-1/#comment-210</link>
		<dc:creator>Benjamin Muskalla</dc:creator>
		<pubDate>Sat, 25 Jul 2009 13:47:26 +0000</pubDate>
		<guid isPermaLink="false">http://www.vogella.de/blog/?p=585#comment-210</guid>
		<description>Lars, you should at least mention the quick fix to turn a concatenated string into a StringBuffer/Builder</description>
		<content:encoded><![CDATA[<p>Lars, you should at least mention the quick fix to turn a concatenated string into a StringBuffer/Builder</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Wanja</title>
		<link>http://www.vogella.de/blog/2009/07/19/java-string-performanc/comment-page-1/#comment-176</link>
		<dc:creator>Wanja</dc:creator>
		<pubDate>Tue, 21 Jul 2009 09:20:22 +0000</pubDate>
		<guid isPermaLink="false">http://www.vogella.de/blog/?p=585#comment-176</guid>
		<description>@David Karr
thnx for the correction and the good explanation.</description>
		<content:encoded><![CDATA[<p>@David Karr<br />
thnx for the correction and the good explanation.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Kirk</title>
		<link>http://www.vogella.de/blog/2009/07/19/java-string-performanc/comment-page-1/#comment-175</link>
		<dc:creator>Kirk</dc:creator>
		<pubDate>Tue, 21 Jul 2009 07:26:12 +0000</pubDate>
		<guid isPermaLink="false">http://www.vogella.de/blog/?p=585#comment-175</guid>
		<description>Look at the byte code and then dump the assembler from the JIT. You&#039;ll see that the compilers don&#039;t always get it right. That said, I see little reason to use StringBuffer. You take the synchronization hit and most likely you&#039;re going to only use the values in a local context. 

This benchmark was vetted by several people over a period of a couple of months http://www.infoq.com/articles/java-threading-optimizations-p1.

Kirk</description>
		<content:encoded><![CDATA[<p>Look at the byte code and then dump the assembler from the JIT. You&#8217;ll see that the compilers don&#8217;t always get it right. That said, I see little reason to use StringBuffer. You take the synchronization hit and most likely you&#8217;re going to only use the values in a local context. </p>
<p>This benchmark was vetted by several people over a period of a couple of months <a href="http://www.infoq.com/articles/java-threading-optimizations-p1" rel="nofollow">http://www.infoq.com/articles/java-threading-optimizations-p1</a>.</p>
<p>Kirk</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: hel</title>
		<link>http://www.vogella.de/blog/2009/07/19/java-string-performanc/comment-page-1/#comment-171</link>
		<dc:creator>hel</dc:creator>
		<pubDate>Mon, 20 Jul 2009 23:11:47 +0000</pubDate>
		<guid isPermaLink="false">http://www.vogella.de/blog/?p=585#comment-171</guid>
		<description>&gt; Vogella says: if you require thread safety you should use StringBuffer.

It&#039;s only an issue if you reuse StringBuffers/-Builders stored in member variables.

Usually you don&#039;t reuse StringBuffers/-Builders. You create them in local scope and forget about them when leaving a method, like this:

public String toString() {
   // Creating objects is very, very cheap.
   return new StringBuilder()
      .append(name)
      .append(&quot;=&quot;)
      .append(value)
      .toString();
}

the several threads scenario: you have several threads building a string with a single StringBuffer ... this can&#039;t be good. The resulting string is not determined. You need external synchronisation to avoid race conditions. But if you synchronize the access to StringBuffer you should use StringBuilder instead.</description>
		<content:encoded><![CDATA[<p>&gt; Vogella says: if you require thread safety you should use StringBuffer.</p>
<p>It&#8217;s only an issue if you reuse StringBuffers/-Builders stored in member variables.</p>
<p>Usually you don&#8217;t reuse StringBuffers/-Builders. You create them in local scope and forget about them when leaving a method, like this:</p>
<p>public String toString() {<br />
   // Creating objects is very, very cheap.<br />
   return new StringBuilder()<br />
      .append(name)<br />
      .append(&#8220;=&#8221;)<br />
      .append(value)<br />
      .toString();<br />
}</p>
<p>the several threads scenario: you have several threads building a string with a single StringBuffer &#8230; this can&#8217;t be good. The resulting string is not determined. You need external synchronisation to avoid race conditions. But if you synchronize the access to StringBuffer you should use StringBuilder instead.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: vogella</title>
		<link>http://www.vogella.de/blog/2009/07/19/java-string-performanc/comment-page-1/#comment-168</link>
		<dc:creator>vogella</dc:creator>
		<pubDate>Mon, 20 Jul 2009 21:05:23 +0000</pubDate>
		<guid isPermaLink="false">http://www.vogella.de/blog/?p=585#comment-168</guid>
		<description>@David thank you for the clarifcation about the compilers.</description>
		<content:encoded><![CDATA[<p>@David thank you for the clarifcation about the compilers.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: David Karr</title>
		<link>http://www.vogella.de/blog/2009/07/19/java-string-performanc/comment-page-1/#comment-167</link>
		<dc:creator>David Karr</dc:creator>
		<pubDate>Mon, 20 Jul 2009 20:58:32 +0000</pubDate>
		<guid isPermaLink="false">http://www.vogella.de/blog/?p=585#comment-167</guid>
		<description>@Vogella:
The Java Language Specification does not specify this behavior, but both the Sun and Eclipse Java compilers do this (converting &quot;+&quot; to StringBuilder usage).  It&#039;s reasonable to assume all modern Java compilers do this.

@Wanja:
Your first statement is slightly misleading. If you&#039;re actually concatenating hard-coded strings, the compiler doesn&#039;t convert it to a StringBuilder, it actually converts it directly to a string, as the compiler can do the entire computation at compile time.

Concerning the several comments about thread safety:
I think it&#039;s very unlikely the &quot;lack of thread safety&quot; of StringBuilder would ever be an issue for anyone. This would be an issue if you had the same StringBuilder instance being accessed by multiple threads at the same time. This would be a very odd situation.</description>
		<content:encoded><![CDATA[<p>@Vogella:<br />
The Java Language Specification does not specify this behavior, but both the Sun and Eclipse Java compilers do this (converting &#8220;+&#8221; to StringBuilder usage).  It&#8217;s reasonable to assume all modern Java compilers do this.</p>
<p>@Wanja:<br />
Your first statement is slightly misleading. If you&#8217;re actually concatenating hard-coded strings, the compiler doesn&#8217;t convert it to a StringBuilder, it actually converts it directly to a string, as the compiler can do the entire computation at compile time.</p>
<p>Concerning the several comments about thread safety:<br />
I think it&#8217;s very unlikely the &#8220;lack of thread safety&#8221; of StringBuilder would ever be an issue for anyone. This would be an issue if you had the same StringBuilder instance being accessed by multiple threads at the same time. This would be a very odd situation.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: vogella</title>
		<link>http://www.vogella.de/blog/2009/07/19/java-string-performanc/comment-page-1/#comment-166</link>
		<dc:creator>vogella</dc:creator>
		<pubDate>Mon, 20 Jul 2009 20:43:30 +0000</pubDate>
		<guid isPermaLink="false">http://www.vogella.de/blog/?p=585#comment-166</guid>
		<description>@Chris I find it safer to use the correct Java construct rather then relying on the compiler. But  thank you for the information, it is good to know that the compiler takes care of the simple case. Does only the Sun compiler does this or all?</description>
		<content:encoded><![CDATA[<p>@Chris I find it safer to use the correct Java construct rather then relying on the compiler. But  thank you for the information, it is good to know that the compiler takes care of the simple case. Does only the Sun compiler does this or all?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: vogella</title>
		<link>http://www.vogella.de/blog/2009/07/19/java-string-performanc/comment-page-1/#comment-164</link>
		<dc:creator>vogella</dc:creator>
		<pubDate>Mon, 20 Jul 2009 20:35:50 +0000</pubDate>
		<guid isPermaLink="false">http://www.vogella.de/blog/?p=585#comment-164</guid>
		<description>If you require thread safety you should use StringBuffer</description>
		<content:encoded><![CDATA[<p>If you require thread safety you should use StringBuffer</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Chris Kessel</title>
		<link>http://www.vogella.de/blog/2009/07/19/java-string-performanc/comment-page-1/#comment-163</link>
		<dc:creator>Chris Kessel</dc:creator>
		<pubDate>Mon, 20 Jul 2009 19:57:37 +0000</pubDate>
		<guid isPermaLink="false">http://www.vogella.de/blog/?p=585#comment-163</guid>
		<description>This is far too simplistic an analysis. The compiler is smart enough to use StringBuilder for concatenations in most cases. Unless you&#039;re concatenating over several iterations in a loop, the compiler is going to take care of this for you. A local JUG member ran tests on stuff like this and looked at the bytecode to determine behavior.

// The compiler takes care of this with StringBuilder for you.
String s = &quot;asdfa&quot; + &quot;Asdfwe2234&quot; + someOtherStringObject;

// The compiler can&#039;t figure this out, so use StringBuilder.
String output = &quot;base&quot;
for ( String s : SomeList )
{
   output = output + s;
}</description>
		<content:encoded><![CDATA[<p>This is far too simplistic an analysis. The compiler is smart enough to use StringBuilder for concatenations in most cases. Unless you&#8217;re concatenating over several iterations in a loop, the compiler is going to take care of this for you. A local JUG member ran tests on stuff like this and looked at the bytecode to determine behavior.</p>
<p>// The compiler takes care of this with StringBuilder for you.<br />
String s = &#8220;asdfa&#8221; + &#8220;Asdfwe2234&#8243; + someOtherStringObject;</p>
<p>// The compiler can&#8217;t figure this out, so use StringBuilder.<br />
String output = &#8220;base&#8221;<br />
for ( String s : SomeList )<br />
{<br />
   output = output + s;<br />
}</p>
]]></content:encoded>
	</item>
</channel>
</rss>

