Results 1 to 5 of 5
  1. #1
    jon80's Avatar
    jon80 is offline Senior Member
    Join Date
    Feb 2008
    Location
    Malta (EU)
    Posts
    211
    Rep Power
    7

    Default [SOLVED] [newbie] variable declaration outside of a loop

    Is it a performance issue if I declare a method outside of a for loop rather than within it?

    For example, I'd rather use this...
    Java Code:
    String _name = null
    for (Method m : _methods) 
    {
       _name = m.getName();
    }
    
    ...than this...
    for (Method m : _methods)
    		{
    			Class _returnType = m.getReturnType();
    			String _name = m.getName();
    		}
    I think yes because it would avoid initializing the variable over and over, so if my guess is correct I would appreciate some technical details on what is happening in the background.

  2. #2
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    If I understand what you want correctly, then yes, it should be fine. The code you posted will not do much, as with the first loop you will only be able to access the name of the last method, and with the second loop you will not be able to access the name outside the loop.

    Technical details... I am more familiar with the for(int i=0; i<upto; i++) form of the loop, however, I believe the form you are using goes through the array, changing the value of m every time it goes through.
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

  3. #3
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,798
    Rep Power
    19

    Default

    You're overanalysing. Micro-performance stuff is seldom worth it.

    I doubt there's a great deal, if any, difference between the two...however they "say" different things. The former implies you are planning on using _name outside the loop, and the latter firmly places it as a variable within the loop.

    So, if _name is only going to be used in the loop then the latter is the better code.

  4. #4
    jon80's Avatar
    jon80 is offline Senior Member
    Join Date
    Feb 2008
    Location
    Malta (EU)
    Posts
    211
    Rep Power
    7

    Default

    Yes, that's syntactically correct, my concern was about the performance issue i.e. Whether the instantiation of a class would result in performance issues.

    It's true I tend to analyze every bit and piece when I'm in "development mode" :)

    Thanks for the helpful feedback :)

  5. #5
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,798
    Rep Power
    19

    Default

    Quote Originally Posted by jon80 View Post
    Yes, that's syntactically correct, my concern was about the performance issue i.e. Whether the instantiation of a class would result in performance issues.

    It's true I tend to analyze every bit and piece when I'm in "development mode" :)

    Thanks for the helpful feedback :)
    But you're not instantiating it...so no problem there.

    In any case, you should always think in terms of legibility. Your code should look like it does what it's supposed to do...so, in the first case, if your code does not use _name outside the loop then anyone coming along later to make changes will envounter a variable that is declared in the wrong scope...and possibly spend a bit of time wondering whether that was intentional, or has some meaning.

    This is why micro-optimisation is usually a waste...do the code, make it functional, legible, maintainable...then if it runs like poo try and figure out why. The odds are it has nothing to do with whether you declared a String inside a loop rather than outside.

Similar Threads

  1. Newbie having problems with for loop
    By Dannii in forum New To Java
    Replies: 4
    Last Post: 04-13-2009, 11:52 PM
  2. Variable not initialized after while loop
    By ejs7597 in forum New To Java
    Replies: 6
    Last Post: 02-28-2009, 05:00 AM
  3. [SOLVED] Referencing Variable within Loop
    By Judoon_Platoon in forum Java Applets
    Replies: 2
    Last Post: 05-22-2008, 11:54 AM
  4. Declaration
    By asifahmed in forum New To Java
    Replies: 1
    Last Post: 04-05-2008, 05:38 AM
  5. multiple declaration of a variable
    By eva in forum New To Java
    Replies: 2
    Last Post: 01-28-2008, 09:17 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •