I got a suggestion or requirement in my last nitpick. I'm a little confused about why I would want to combine the statement below. If I combine the two statements, wouldn't that mean that the variable is being initialized each time through the loop? Thanks for any clarification. <Nitpick> int i = 0; for ( i = 0; i < 100; i++ ) Why not combine the above two lines? </Nitpick>
I'm a Java newbie too, so my explanation may not be all that accurate, but the way I understand it, you can combine the statements because you're just setting the parameters of the loop. If you were initializing the variable each time, you would also be setting it to zero each time, which you're obviously not. Does that make sense? Maybe someone can give you a more legitimate answer.
Hi Brian I'm also fairly new to java but the construct is the same with most languages. The i=0 part of the loop statement is set only once as the initial value to evaluate the loop condition against. That's why it's ok to declare and initialize it in the for... statement. Also, by initializing in the for... statement, i's scope is only within that loop and is collected by the garbage collector after the loop finishes. Using i as a loop counter is standard practice. If you declare the i outside the loop, you could cause problems with other loops using i due to the scope problem. Does that help?
Good explanation Brad. If you need to keep the value of the counter when the loop finishes, then what you did would be exceptable. When you leave the loop, you can still access i. However, for this assignment, you don't need i any more, so declaring it inside of the for construct is more common and better for the sake of memory. I know here it doesn't really matter becuase the class is so small, but we are trying to teach not just this one example, but show you what would be needed for larger programs that you will have in the future. So let's say you have a class that will need to have 4 separate loops. You would have to keep resetting i back to 0 for each loop and it would be prone to bugs if that was forgotten, something that may be hard to catch. Keep you counter inside of your loop, then once you leave it you can't get to it anymore so next loop you have you can declare i again without any problems. Does that make sense? Now if you don't know how to declare it inside of a loop, then take a look at some books or code examples around this forum. I could tell you, but I always feel you will remember more if you try to look things up yourself. Or at least that is how I learn Bill
I for one did not know that Java's garbage collector cleaned up the memory used by the counter variable once the loop was completed. That makes a lot of sense and really underlines why you wouldn't want to create variables for counters outside of the loop they are used in. Thanks! Mike
Joined: Oct 26, 2000
Thanks for the clarification. It makes a lot more sense now. I was thinking that you were initializing the variable each time through the loop, which made it seem like there would be a performance hit. It sounds like the opposite is true, as far as performance goes.
Joined: Jun 30, 2000
Yep, when you initialize a loop variable, inside of the loop construct, it is only initialized once there. Bill