Is there any performance penalty for the following code snippet?
for (int i=0; i<someValue; i++) { Object o = someList.get(i); o.doSomething; }
Or does this code actually make more sense?
Object o; for (int i=0; i<someValue; i++) { o = someList.get(i); o.doSomething; }
If in byte code these two are totally equivalent then obviously the first method looks better in terms of style, but I want to make sure this is the case.
Declaring variables inside or outside of a loop, It's the result of JVM specifications But in the name of best coding practice it is recommended to declare the variable in the smallest possible scope (in this example it is inside the loop, as this is the only place where the variable is used).
You CAN use a loop. The trick is that you have to save the reference to each one as you create it. A simple way would be to use an array. You have to declare the array outside the loop, then use your loop counter as the index into the array...
You can't create an object outside of any method. And objects aren't local, global, instance scope of any of that -- only their references have such attributes.
Creating an Object Declaration − A variable declaration with a variable name with an object type. Instantiation − The 'new' keyword is used to create the object. Initialization − The 'new' keyword is followed by a call to a constructor. This call initializes the new object.
If you do a 1000-2000 loop using the two methods, you will find out that declaring it outside is more economical and better optimized. Reason being, re-initialization of the variable in the loop leads to creation of memory location (different ones) for the variable.
Declaring String str outside of the while loop allows it to be referenced inside & outside the while loop. Declaring String str inside of the while loop allows it to only be referenced inside that while loop. Show activity on this post.
So unless you will need the same variable outside the loop (or if each iteration depends on an operation done to that variable in the previous iteration), it's preferable to declare the scope within which it is used.
Conclusion: Depending of the size of the local variable, the difference can be huge, even with not so big variables. Just to say that sometimes, outside or inside the loop DOES matter. Show activity on this post. You have a risk of NullPointerException if your calculateStr () method returns null and then you try to call a method on str.
In today's compilers, no. I declare objects in the smallest scope I can, because it's a lot more readable for the next guy.
To quote Knuth, who may be quoting Hoare:
Premature optimization is the root of all evil.
Whether the compiler will produce marginally faster code by defining the variable outside the loop is debatable, and I imagine it won't. I would guess it'll produce identical bytecode.
Compare this with the number of errors you'll likely prevent by correctly-scoping your variable using in-loop declaration...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With