Where to declare the counter of the most deeply nested loop in Java?

No, it doesn't, and it's good practice to declare the variable in as limited a scope as possible - it makes it clearer for readability. It also ensures that you're not accidentally using the value from the end of one loop at the start of the next.

Up vote 1 down vote favorite share g+ share fb share tw.

I am new to Java and I am building small school project for android. If I have a nested for loop: for (int I = 0; I It is school project so I have to write code that uses smallest amount of memory. Thank you!

Java homework for-loop nested counter link|improve this question edited Mar 5 '11 at 0:57corsiKa24.4k42353 asked Mar 4 '11 at 21:50Shvetusya1248 92% accept rate.

1 Keep you code as it is. The compiler takes care of things like this. Pulling "j" out of the inner loop might actually make things worse – iluxa Mar 4 '11 at 21:53.

No, it doesn't, and it's good practice to declare the variable in as limited a scope as possible - it makes it clearer for readability. It also ensures that you're not accidentally using the value from the end of one loop at the start of the next. (Personally I'd be wary of anyone forcing you to use the absolute minimum amount of memory instead of concentrating on readability and measured optimization where it matters.

).

Thank you for your quick respond! Exactly what I wanted to know! – Shvetusya Mar 5 '11 at 7:23.

Declaring nested variables has no affect on performance, in fact it is recommended so that the variable's use is restricted to the correct scope. This just helps avoid simple mistakes.

The only way to now is to try it different ways. I can tell you that it is more than likely not going to be a performance problem, and generally you don't worry about optimizing until it is a known problem.

No it does not affect performance. The way you wrote it is the best way: always declare a variable so that its scope is no larger than necessary. And let the compiler optimize performance.

There is a performance impact for allocating the integer. However, it is so ridiculously small that I wouldn't bother doing it different. A well-known saying goes "premature optimization is the root of all evil".

EDIT: tested it with System.nanoTime() after Jon's comment. Not one bit of difference whether you "pre-declare" it or not. So I was wrong about there being any impact.

2 The integer isn't "allocated" on each outer loop iteration. The stack frame will be allocated with enough space for both variables when the method is entered. (I agree that if there were any performance difference it probably still wouldn't be worth worrying about, but it's important to understand that your first sentence is incorrect.

Edit appropriately and I'll remove the downvote :) – Jon Skeet Mar 4 '11 at 21:54 @Jon I didn't know that! Cheerio. – G_H Mar 4 '11 at 21:55 @Jon Keep the downvote.

It clearly shows that the first sentence is a misconception. And now I'm getting interested in learning JVM and compiler internals. – G_H Mar 4 '11 at 22:21.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions