Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to avoid new objects

 
Josue Cedeno
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is an extension of my previous question. When dealing with immutability, what is the best method for dealing with potential multiples.
I'm working on "Times" and I want to avoid wasting resources as much as possible. Any general tips?
 
Michael Pearson
Ranch Hand
Posts: 351
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I will tread as far as possible from writing about specific Cattle Drive assignments.
-----
First, if output is generated with the "print as you go" technique new objects aren't created.
If for some reason text has to be concatenated take a look at the class StringBuffer, since it provides this functionality without creating a new object.
I found the Str class in the JavaRanchCommon package interesting. It combines functionality of String and StringBuffer in a usable way.
[ April 08, 2002: Message edited by: Michael Pearson ]
 
Carol Murphy
village idiot
Bartender
Posts: 1203
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Michael Pearson:

First, if output is generated with the "print as you go" technique new objects aren't created.
If for some reason text has to be concatenated take a look at the class StringBuffer, since it provides this functionality without creating a new object.

Please elaborate. I don't quite understand what you mean here. New String objects are created every time you concatenate, but not with StringBuffer? And what technique is "print as you go" ?
 
jason adam
Chicken Farmer ()
Ranch Hand
Posts: 1932
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Print as you go - instead of building a String by doing a bunch of concatenations, you simply call System.out.print() or println() in your method(s) where ever you need printing done.
 
Michael Pearson
Ranch Hand
Posts: 351
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where is Marilyn?
I'm not going to take credit for something she emphasized so well to me the past 12+ months.
Print as you go just sends the String to output instead of returning it to the calling method. Why call a method and have it return a String to concatenate when you can print the String?
Here's some pseudo code:

[ April 08, 2002: Message edited by: Michael Pearson ]
 
Marilyn de Queiroz
Sheriff
Posts: 9067
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Strings are immutable. That means they cannot be changed. Every String is a new object. String + String creates 4 objects in the process of creating a new String object which is the result.

StringBuffer, on the other hand, is a mutable object. It can be changed. It does not create new objects in the process of changing.

In addition to Michael's example, remember that a statement like:
System.out.println( text + moreText + moreAndMoreText ) ;
can also be written as:
System.out.print( text );
System.out.print( moreText );
System.out.println( moreAndMoreText );

Of course this can be carried to an extreme where readability is diminished.

Printing on the spot definitely beats accumulating and tracking Strings.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic