Declare an object inside or outside a loop?

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

Is there any performance penalty for the following code snippet?

for (int i=0; i         


        
16条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-07 18:09

    I've got to admit I don't know java. But are these two equivalent? Are the object lifetimes the same? In the first example, I assume (not knowing java) that o will be eligible for garbage collection immediately the loop terminates.

    But in the second example surely o won't be eligible for garbage collection until the outer scope (not shown) is exited?

提交回复
热议问题