• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Problem 1.1

 
Ranch Hand
Posts: 290
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I recieved this comment in regards to my code: Rather than accumulating the string, why don't you just print it as you
go along?
Here's the code:
Note: userName is used to store the name entered at the command prompt into the args[] and name is a StringBuffer to add both the userName and a space 100 times.
<code>
for( int i=0; i < 100; i++ )
{
name.append( userName + " ");
}
System.out.print( name.toString() );
</code>
Does that mean do to this:
<code>
for( int i=0; i<100; i++ )
{
System.out.print( userName );
}
</code>
In the first code I used a StringBuffer,I appended the 100 names and spaces together then I printed. I was under the impression that calling System.out.print is expensive which is why I only wanted to call on it once in my top code. Or am I just totally miss understanding what she meant (I think I am).
Thanks,
Joe
 
Ranch Hand
Posts: 1070
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I believe that it clutters up your code more to create a large stringbuffer and then print it out, and doing 100 appends is more work than 100 prints.
Bill
 
Trailboss
Posts: 23778
IntelliJ IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So rather than just print it, you want to build a huge string and then print it too.
Sure, StringBuffer is one object, but it also contains an object (array of characters). And every time you exceed the amount of storage it has, you have to create another char array and copy all of the old contents to the new array.
It seems far more efficient to just write it out.
 
Joseph Russell
Ranch Hand
Posts: 290
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks. Although I did create it by setting the size therefore it wouldn't ever need to resize itself. Knowing that it would print a hundred names...I took the variable for the name added a space and multiplied it by a hundred to get the size of the StringBuffer. How does one find how a method is more expensive than another method?
Joe
 
paul wheaton
Trailboss
Posts: 23778
IntelliJ IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is something you learn through experience.
With the cattle drive, readability comes before performance. If you can get both, that's great.
In this case, performance gains are debatable at best, but you sacrificed readability by complicating your code.
 
Curse your sudden but inevitable betrayal! And this tiny ad too!
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic