This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Declare variable in a loop - Good practice? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Declare variable in a loop - Good practice?" Watch "Declare variable in a loop - Good practice?" New topic
Author

Declare variable in a loop - Good practice?

James Gordon
Ranch Hand

Joined: Aug 09, 2002
Posts: 106
Hi,
Should I always use Method B or it doesn't really matter?

Method A
--------
for (int i=0; i<j; i++) {
int aNumber = i;
}
Method B
--------
int aNumber = 0;
for (int i=0; i<j; i++) {
aNumber = i;
}
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
If you need the variable after the loop has finished, then you had better declare it before the loop. Otherwise it disappears when the loop is exited.
-Barry


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Barry Gaunt:
If you need the variable after the loop has finished, then you had better declare it before the loop. Otherwise it disappears when the loop is exited.
-Barry

Quite correct!
And if you don't need the variable outside the loop, I think it is good practice to declare it inside the loop...
See http://www.refactoring.com/catalog/reduceScopeOfVariable.html and http://www.refactoring.com/catalog/replaceAssignmentWithInitialization.html


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
James Gordon
Ranch Hand

Joined: Aug 09, 2002
Posts: 106
Hi,
Thanks for the response.
I was with the impression that declaring variables outsite a loop is always better since it saves the memory usage ... guess I am actually wrong all this while ...
Jamie Robertson
Ranch Hand

Joined: Jul 09, 2001
Posts: 1879

In general, write the code that makes the makes the most sense when reading/writing it. I say this for 3 reasons:

1. with all the "smart" compiler and JIT run-time optimizations that happen after you write your code, it will probably execute the same code regardless of how it is typed in.
2. If you feel that the complete application you write does not meet speed specifications, then try to optimize the code for speed with the help of a profiling tool. You'll probably find other places where the code is causing the slow down, other than creation of a primitive in a loop.
3. highly optimized code is not always intuitively obvious, readable or maintainable.
So, as Ilja always says, The first rule of code optimization is DON'T!
Jamie
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Jamie Robertson:
So, as Ilja always says, The first rule of code optimization is DON'T!

YEAH! Thanks for assisting...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Declare variable in a loop - Good practice?
 
Similar Threads
can we store an integer in a byte array
Nested for Loop doubt?
Inner Loop
Trouble with compiling?
singleton vs static methods