Is declaring a variable inside a loop is good or declaring on the fly optimal in Java.Also is there any performance cost involved while declaring inside the loop?
eg
Well, if you are worrying about optimizing that code - I'm not sure about how Java evaluates for loops, but having the list.size()
being called inside the loop declaration maybe less efficient than outide the loop (and setting it to a varable listLength
perhaps). I'm pretty sure that method is quicker in JavaScript. The reason it might be more efficient is that having the size function call inside the for loop means it would have to go call the function each time it runs the test to see if the loop is finished, instead of testing against a static value.