++i 与 i++ 区别及踩过的坑

10 篇文章 0 订阅

++i 与i++的区别是前者是先运算再取值,后者是先取值在运算

Section1

public class Test {
    public static void main(String args[]){
        int i = 0;
        CalculateBean test = new CalculateBean();
        test.changeIndex(i++);
    }
}
 
public class CalculateBean {
    public void changeIndex(int index){
        System.out.println("the value is "+index);
    }
}

问,打印结果是0还是1?
 
我们run一下,
答案是
the value is 0
==============================================================
Section2
 
修改一下
public static void main(String args[]){
    int i = 0;
    CalculateBean test = new CalculateBean();
    for (int k = 0;k<10;k++){
        test.changeIndex(i++);
    }
}
打印结果是
the value is 0
the value is 1
the value is 2
the value is 3
the value is 4
the value is 5
the value is 6
the value is 7
the value is 8
the value is 9
-----------------------------------------
虽然在传入参数的时候i++了,但是仍然是从0开始的;
 
Section3
i++和++i的区别虽然比较简单,但是在开发的时候有的时候不注意,就有可能用错,
而且这种错误不注意还发现不了。。。
 
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    loadData(currentPage);
    initLoadMoreListener();
.......
 
 
protected int currentPage = 1;
private void doLoadMore() {
    loadData(currentPage++);
}

 
像最近遇到的这个bug,page 1 加载了两次,就是由于这个地方没有注意造成的。
 
 
 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值