java中i++与++i的区别

i++、++i的相同点在于都相当与i=i+1,不同点是i++是先运算再加1,而++i是先加1再运算。

示例

小贴士

I=i+j可以替代为i+=j,推荐使用后者,具有更好的性能。

自加运算运行原理:

i=i+j和i+=j是有区别的,在内存当中每一个变量都有一个存储空间,i=i+j是将i从内存当中取出,然后执行i+j然后再将结果赋值给i,而i+=j是不需要取出的,而是直接在i的基础上加上j;但是两者的结果是相同的,只是执行次数不一样。

未经允许不得转载:陈海飞博客 » java中i++与++i的区别

分享到:更多 ()

评论 1

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
  1. #1

    I am only writing to make you know of the nice encounter my wife’s daughter obtained reading yuor web blog. She even learned many issues, with the inclusion of what it is like to have a wonderful giving mood to let most people with no trouble fully grasp certain hard to do subject matter. You truly surpassed our own expected results. Many thanks for delivering such useful, healthy, explanatory not to mention fun tips about your topic to Lizeth.

    adidas nmd11小时前回复