Efficiency of Java code with primitive types

前端 未结 9 871
遥遥无期
遥遥无期 2021-01-03 12:18

I want to ask which piece of code is more efficient in Java? Code 1:

void f()
{
 for(int i = 0 ; i < 99999;i++)
 {
  for(int j = 0 ; j < 99999;j++)
  {         


        
9条回答
  •  春和景丽
    2021-01-03 12:47

    The second is worse.

    Why? Because the loop variable is scoped outside the loop. i and j will have a value after the loop is done. Generally that isn't what you want. The first scopes the loop variables so it's only visible within the loop.

提交回复
热议问题