Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Declare an object inside or outside a loop?

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.

like image 980
Yuval Adam Avatar asked Dec 18 '08 13:12

Yuval Adam


People also ask

Should you declare variables inside or outside a loop?

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).

Can you create objects in a loop?

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...

Can object be created outside method?

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.

How objects are declared?

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.

Is it better to declare variables inside or outside a loop?

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.

Why do we declare string outside of the while loop?

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.

When to declare the scope of a variable in a loop?

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.

Does it matter if you call a method inside or outside loop?

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.


2 Answers

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.

like image 189
Dave Markle Avatar answered Sep 18 '22 18:09

Dave Markle


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...

like image 43
Dan Vinton Avatar answered Sep 17 '22 18:09

Dan Vinton