Declare an object inside or outside a loop?

前端 未结 16 838
广开言路
广开言路 2020-12-07 17:15

Is there any performance penalty for the following code snippet?

for (int i=0; i         


        
16条回答
  •  感动是毒
    2020-12-07 17:55

    Don't prematurely optimize. Better than either of these is:

    for(Object o : someList) {
        o.doSomething();
    }
    

    because it eliminates boilerplate and clarifies intent.

    Unless you are working on embedded systems, in which case all bets are off. Otherwise, don't try to outsmart the JVM.

提交回复
热议问题