• 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

How to avoid new objects

 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 351
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
village idiot
Posts: 1208
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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" ?
 
Chicken Farmer ()
Posts: 1932
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Get me the mayor's office! I need to tell her about this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic