#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2004
    Posts
    245
    Rep Power
    10

    Variable inside or outside of the loop?


    Typically, a variable is used in a loop. For instance,

    String label;

    while(true){
    ...
    label = new String("xyz");
    }

    or

    while(true){
    ...
    String label = new String("xyz");
    }

    Is there any efficiency difference, or preferred way to define the variable?

    Thanks.
  2. #2
  3. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,696
    Rep Power
    347
    Depends on the requirements. If the variable is only used inside the loop, then it should be defined inside the loop so that it goes away when the execution exits the loop.

IMN logo majestic logo threadwatch logo seochat tools logo