Results 1 to 6 of 6
- 06-06-2010, 12:01 AM #1
String/ object creation/ efficiency
Suppose you have some code that looks like this
Java Code:StringBuilder str = new StringBuilder(); str.append("one"); str.append("two"); System.out.println(str.toString() + "three"); System.out.println(str.toString() + "four"); System.out.println(str.toString() + "five");
Question 1: In this example, str.toString() conversions happens 3 times separately, or Java knows to reuse the results of conversion done once locally?
Question 2. Would any efficiencies be introduced by changing the code to
Please let me knowJava Code:StringBuilder str = new StringBuilder(); str.append("one"); str.append("two"); String x = str.toString(); System.out.println(x + "three"); System.out.println(x + "four"); System.out.println(x + "five");
- 06-06-2010, 02:06 AM #2
There is a compiler option that will show you the code that is generated. If you want to know what the compiler is generating look at that.
Calling toString() once should be more efficient. See above
- 06-06-2010, 04:33 AM #3
In the first code, str.toString() will be executed 3 times. The compiler cannot cache the return value -- what happens if str is modified by another thread between calls?
db
- 06-06-2010, 10:39 AM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,402
- Blog Entries
- 7
- Rep Power
- 17
Well, if that str is just a local variable no other threads can touch it so theoretically (if the compiler were that smart) those three toString( ... ) could've been collapsed to just one; but then again: the compiler doesn't know that the toString() method is referential transparent so it duly generates three method calls ...
kind regards,
Jos
- 06-06-2010, 02:05 PM #5
- 06-06-2010, 02:57 PM #6
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,402
- Blog Entries
- 7
- Rep Power
- 17
AFAIK the javac compiler is far too nearsighted to check anything like that, i.e. it simply generates the dumb code and doesn't analyze anything; it isn't much of an optimizing compiler. Partly that's because it doesn't know, and can't know anything about, say, referential transparency of methods, i.e. as far as javac is concerned the StringBuilder constructor might start a new Thread that modifies the StringBuilder (therefore forcing three times the toString() method call code to be generated).
kind regards,
Jos
Similar Threads
-
Doubt in Package-Object creation
By t0mat0 in forum New To JavaReplies: 1Last Post: 06-02-2010, 06:39 AM -
String Object Creation
By indranil in forum New To JavaReplies: 1Last Post: 04-13-2010, 11:30 AM -
object creation
By enygma in forum Java AppletsReplies: 0Last Post: 01-05-2010, 10:01 AM -
Object creation and construstion
By abimaran in forum New To JavaReplies: 8Last Post: 12-15-2009, 09:58 AM -
Object creation and memory issues
By bugger in forum New To JavaReplies: 11Last Post: 11-29-2007, 12:56 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks