通俗易懂的a++和++a的解释

时间:2021-04-15 12:49:34   收藏:0   阅读:0

public class Test {
public static void main(String[] args) {
int a = 3;
int b = a++; //a先给b赋值,所以b是3,然后再自增1,所以再输出a为4
System.out.println(a); //输出结果 4
int c = ++a; //a先自增1,因为上面a已经为4,然后把值赋给了c,所以c为5,
//a++,先赋值,再自增
//++a,先自增,再赋值

    System.out.println(a);  //输出结果 5
    System.out.println(b);  //输出结果 3
    System.out.println(c);  //输出结果 5

}

}

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!