This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
I don't understand how you can declare a new "userGuess" variable each time through the loop and not overflow memory somewhere. Why isn't "userGuess" declared at the top of the class with all the other variables?
a variable goes out of scope when you hit the closing brace of the block where it's declared. So, technically, the variable goes out of scope each time through the loop and is eligible for garbage collection.
As to why it's not declared elsewhere... that's a style choice. a lot of folks believe that it's best to not declare it until it's needed. Why clutter up the top of your program with all this stuff you don't need? Also, it can be frustrating to see
with the "String userGuess" 200 lines above here. The first thing i'd think is "What the heck kind of varibleis userGuess? A String? A StringBuffer? A Character?"
There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Something similar came up a few months back. Somebody tried looking at the bytecode.
Write out a class with two tiny methods one with a variable declared inside a loop and one with the variable declared just before the loop. Don't write anything else; you need the class to be very small.
Don't execute it, but use the javap tool "javap -c MyClass"
That expands the bytecode and tells you what it means. If you have ever read assembler, it looks very similar.
Compare the two methods.
I forget who it was tried that, but they said the two were identical in the bytecode. I haven't tried it myself. It would appear there is an optimisation in the javac tool which irons out that difference.
Joined: Dec 04, 2008
So even though you don't exit the loop, the very fact of getting to the closing brace causes variables declared in the loop to go out of scope. That's good to know. But that would seem to cause extra overhead.
I'm not sure I'd trust a loop test that didn't do anything, to show that there is no performance hit when declaring a var within the loop vs outside. A good compiler will see when a var is not being used, or is redundant to another var, and optimize it out.
Thanks for the quick answer.
Joined: Oct 13, 2005
The problem with having lots inside your loop is that the printout will overwhelm the scrolling capacity of the command line window. You can try running the loop 1000000 times and using the two methods in the System class which time things (the nanosecond method is probably more appropriate), to see how long the loop takes. Remember a local variable reference doesn't live on the heap; it has a fleeting existence on the stack, and only stays in memory until something else is pushed onto its former location on the stack. Then the objects may have no more references to them and are eligible for garbage collection. The overhead of garbage collection is slight, and is outweighed by the problem of programmers organising their own memory management.