• 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

do methods that return String create them on the heap?

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
for example :

does that create a new object on the heap for every call to request.getParameter? therefore it would be better to only create 1 object on the heap by doing:


and does that apply to any method that returns a String/other object?

thanks





 
Marshal
Posts: 28226
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No. Or rather, the question is wrong. Methods don't return Strings, or any other objects. Methods return references.

You can't tell from looking at a method whether the reference it returns is a reference to a newly created object or a reference to an object which it just created specifically for the purpose. But at any rate the example you give is almost certainly an unnecessary micro-optimization at best.

But didn't we just have a discussion about premature optimization in another thread? It isn't worth making a change which will save a microsecond unless you're going to be executing that code a million times in rapid succession. Please read the Wikipedia article about Program optimization, in particular the section about premature optimization. You should really concentrate your efforts elsewhere.
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Joe,

As is so often the case, the answer is "it depends". Some methods will create new objects, and others won't. For example, HttpServletRequest..getParameter() doesn't; all the parameters are held in a Map inside the request, and getParameter just returns objects that already existed. On the other hand, a method like String.substring() pretty much always returns a new object, as that's what it's designed to do. There are even going to be methods that return new objects only some of the time.

Often, combining common expressions the way you have done here is a good idea even if it really doesn't save any memory or time, because it makes your code easier to read.
 
Joe Pit
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks for the replies guys
 
Paul Clapham
Marshal
Posts: 28226
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's another thing to pay attention to: your original example assumes that

will return the same value from both calls in the original snippet. In many cases that's a reasonable assumption but there are cases where calling a method twice will return two different values. In those cases you would have to respect the design of the method and call it as many times as necessary.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic