Difference between declaring variables before or in loop?

前端 未结 25 2475
长发绾君心
长发绾君心 2020-11-22 02:37

I have always wondered if, in general, declaring a throw-away variable before a loop, as opposed to repeatedly inside the loop, makes any (performance) difference? A (q

25条回答
  •  独厮守ぢ
    2020-11-22 03:30

    this is the better form

    double intermediateResult;
    int i = byte.MinValue;
    
    for(; i < 1000; i++)
    {
    intermediateResult = i;
    System.out.println(intermediateResult);
    }
    

    1) in this way declared once time both variable, and not each for cycle. 2) the assignment it's fatser thean all other option. 3) So the bestpractice rule is any declaration outside the iteration for.

提交回复
热议问题