Pros and Cons of declaring expression in java? “sum += i” instead of “sum = sum + i” [closed]

十年热恋 提交于 2021-02-17 07:07:32

问题


I got this question in an interview that what's the impacts of declaring expression sum+=i; or sum = sum+i; inside the loop.

    int sum = 0;
    for (int i = 0; i <= 100; i++) {
        sum = sum + i; //Expression 1
        sum += i;  //Expression 2
    }

回答1:


In this example, there is no difference whatsoever other than what you consider easiest to read.



来源:https://stackoverflow.com/questions/66162802/pros-and-cons-of-declaring-expression-in-java-sum-i-instead-of-sum-sum

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!