• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

do methods that return String create them on the heap?

 
Joe Pit
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • 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





 
Paul Clapham
Sheriff
Pie
Posts: 20971
31
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • 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.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
thanks for the replies guys
 
Paul Clapham
Sheriff
Pie
Posts: 20971
31
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic