File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes declaring variables Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "declaring variables" Watch "declaring variables" New topic

declaring variables

Steve Trigero

Joined: Dec 04, 2008
Posts: 9
In one of the examples in my Java book, is the following loop:

while( !dotComList.isEmpty() ) {
String userGuess = helper.getUserInput("Enter a guess: ");

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?
fred rosenberger
lowercase baba

Joined: Oct 02, 2003
Posts: 11955

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
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46362
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.
  • Compile it
  • 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.
    Steve Trigero

    Joined: Dec 04, 2008
    Posts: 9
    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.
    Campbell Ritchie

    Joined: Oct 13, 2005
    Posts: 46362
    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.
    BV Boose
    Ranch Hand

    Joined: Jul 26, 2008
    Posts: 33
    I found a good analysis of inside vs outside using primitives and objects. I learned years ago that declaring variables outside the loop was more efficient in performance and memory, guess I need to revisit that notion.
    I agree. Here's the link:
    subject: declaring variables
    jQuery in Action, 3rd edition