java - Is it more efficient to declare a variable inside a loop, or just to reassign it? -
while writing loops confused 1 should choose. example,
int sum; for(int i=0; i<10; i++) { sum=0; ... .... } or
for(int i=0; i<10; i++) { int sum=0; ... .... } say, variable required in loop. there no need of in later part of program. need value of variable sum 0 @ origin of loop. 1 improve practice? re-initializing variable @ start of loop or re-declaring it? 1 more efficient?
if declare variable outside of loop , not utilize past loop, compiler move declaration within loop.
that means there no reason compare efficiency here, since end same exact code jvm run 2 approaches.
so next code:
int sum; for(int i=0; i<10; i++) { sum=0; } ... becomes after compilation:
for(int = 0; < 10; i++) { int sum = 0; } java performance loops initialization declaration
No comments:
Post a Comment