Results 1 to 6 of 6
  1. #1
    mac's Avatar
    mac
    mac is offline Member
    Join Date
    Dec 2009
    Location
    St. Petersburg, Russia
    Posts
    81
    Rep Power
    0

    Default 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

    Java 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");
    Please let me know

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    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

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,184
    Rep Power
    19

    Default

    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

  4. #4
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,371
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Darryl.Burke View Post
    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?
    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

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,184
    Rep Power
    19

    Default

    Quote Originally Posted by JosAH View Post
    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
    Thanks Jos, but does the compiler really look to see that the local variable has in fact been locally assigned to refer to a new object, and not to some existing StringBuilder that might be referred to -- and modified via -- another variable elsewhere?

    db

  6. #6
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,371
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Darryl.Burke View Post
    Thanks Jos, but does the compiler really look to see that the local variable has in fact been locally assigned to refer to a new object, and not to some existing StringBuilder that might be referred to -- and modified via -- another variable elsewhere?
    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

  1. Doubt in Package-Object creation
    By t0mat0 in forum New To Java
    Replies: 1
    Last Post: 06-02-2010, 06:39 AM
  2. String Object Creation
    By indranil in forum New To Java
    Replies: 1
    Last Post: 04-13-2010, 11:30 AM
  3. object creation
    By enygma in forum Java Applets
    Replies: 0
    Last Post: 01-05-2010, 10:01 AM
  4. Object creation and construstion
    By abimaran in forum New To Java
    Replies: 8
    Last Post: 12-15-2009, 09:58 AM
  5. Object creation and memory issues
    By bugger in forum New To Java
    Replies: 11
    Last Post: 11-29-2007, 12:56 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •