tag:blogger.com,1999:blog-3237724005744642470.post1870063128499906660..comments2020-05-08T12:31:27.297+01:00Comments on Captain Debug's Blog: More on String and the StringBuilder PerformanceRoger Hugheshttp://www.blogger.com/profile/07042290171112551665[email protected]Blogger2125tag:blogger.com,1999:blog-3237724005744642470.post-10873887294130408342011-08-15T20:58:12.433+01:002011-08-15T20:58:12.433+01:00I think that the main point I wanted to get over i...I think that the main point I wanted to get over in this blog is that testing such optimisations is difficult as it's impossible to know what else the JVM's doing whilst running your test code. For example, it may be allocating space in <i>new gen</i>, or running a garbage collection thread.Roger Hugheshttps://www.blogger.com/profile/07042290171112551665[email protected]tag:blogger.com,1999:blog-3237724005744642470.post-28105597872650488822011-08-15T17:05:16.723+01:002011-08-15T17:05:16.723+01:00Hi there Captain!
For simple string concatenation...Hi there Captain!<br /><br />For simple string concatenation e.g. where it is on one line, or it is not in a loop, the compiler can (and does) use a String Builder or String Buffer to optimise.<br /><br />So "hello" + " there " + nameOfPerson<br /><br />becomes<br /><br />(new StringBuilder("hello").append(" there ").append(nameOfPerson)).toString()<br /><br />Or similar...<br /><br />http://java.sun.com/docs/books/jls/third_edition/html/expressions.html#15.18.1.2<br /><br />The optimisation is optional so it will depend on the compiler however (not the JVM)<br /><br />All the best<br /><br />AdzAdam Perryhttps://www.blogger.com/profile/16839632259862699051[email protected]