• 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

Reference declaration

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
How time expensive is it to declare references to objects inside methods? Is it better to declare them as private members of the class? What about simple types?

As a very simple example suppose that you inside a method you have five for-loops. what is better?
This:


or this?

 
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Pablo,
the only real difference I can see is that the first code block declares the i loop integer and the Iterator's in the for loops, and the second example declares them as separate instance variables.

I suspect a decent comiler can optimize away the difference.

In theory, when you write
for (int i = 0; i++...)
{}
the compiler treats it the same as
int i = 0;
for (i = 0; i ++...)
{}
anyway, so there would be no difference here.

Also, when you declare a method variable, you have to initialize it, which 'may' cause a time delay for complex objects. In your case, I suspect the compiler will generate exactly the same code for both examples. (You should be able to turn off this optimization in your IDE, but you won't see much of a difference until you start to initialize complex objects that take a measurable amount of time to initialize).

Now, if you declare them in the method, then you can use them after the for loops. If you declare them in the for loops, they will not be available outside of the for loops' scopes.

You also ask if they were to be declared at the class level (outside of methods), what impact that will have? Well, time-wise, I'd say again, very little, but technically instance variables are initialized to their defaults first, so

public class A
{
private int i;
...

}
causes the i variable to be initialized to zero at object creation (before constructor call).

Jeff Walker
 
Ranch Hand
Posts: 1923
Scala Postgres Database Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
call me pedantic, but compilers tend to be pedantic:

In theory, when you write
for (int i = 0; i++...)
{}
the compiler treats it the same as
int i = 0;
for (i = 0; i ++...)
{}
anyway, so there would be no difference here.


The compiler should treat it as:

or

with the scope of 'int i' being reduced to the loop.
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Pablo,

welcome to the Ranch!

Please notice that we do have a naming policy at the Ranch. Please change your display name accordingly - the linked page tells you how to do so.

Thank you very much - and have much fun at the Ranch!
 
Pablo Olmos
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
At run-time, what is better the first example or the second?

Explained in another way, does the program run slower if for each method invoked I declare and use a bunch of intermediate references/variables inside the method? or faster, if some of those references/variables were declared as member of the class that owns the method?

Pablo
 
Stefan Wagner
Ranch Hand
Posts: 1923
Scala Postgres Database Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can write a test to find it out for your JVM and your kind of invoking it. That means, calling 'java -server YourProgram' might behave different, from not using the '-server'-flag.

The Hotspot-optimizing might result in other runtimes.
A 1.4.0 may behave different from a 1.4.2.
A linux-implementation might behave different from a MacOs.

Make a lot of tests, for different counts of calls.
Repeat those tests, to avoid random-results.

Read Knut: 'Premature optimization is the root of all evil'.

Most code needs a long time of maintainance, and variables moved to the class instead of a method:
a) confuse
b) might behave funny with multithreading
c) can easily cause developer-errors

But make some tests, to see, what you have to pay to avoid the drawbacks.
 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First, method variables are stored in the stack frame of your method. So every variable you declare will need a place in this frame. But when variables get out of scope in the method, this place can be used by an other variable that is declared further in the method.

Second, you should keep the scope of your variables as little as ^possible, at all times. If you don't do so, you might cause a STORE operation (for variables with a lifecycle larger than needed), and *that* would be a waste.

So, basically, it is better coding style to declare your variables in your for loop, and not outside. You should not trust the compiler to optimize your code since not all compilers will do so.

The same is true for iterators: you beter use:

for (Iterator=...

instead of:

Iterator iter = ...
while (iter.hasNext()) ...

HTH,
Kris
 
Pablo Olmos
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the advice. It's exactly what I needed to know.

Pablo
 
Ilja Preuss
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Kris Philippaerts:
First, method variables are stored in the stack frame of your method.



I am not sure wether this is guaranteed by the Java specification. It is certainly the most common implementation, but I could also imagine the Hot Spot Engine to decide to use a register for a "very local" variable instead.

But if the stack frame is used, then it needs to be allocated anyway (for the method return address, parameters etc.) and will only be allocated at the method entry point. That is, the declaration of a local variable wouldn't cause any additional operation to be performed at all.
 
reply
    Bookmark Topic Watch Topic
  • New Topic