java中关于++x前加加和x++后加加的疑惑!下面是源代码和运行结果的截图!
public class Test2 {
public static void main(String[] args) {
int x = 8;
//理论上来说x的初始值是8,x先赋值给x,x再自加1,此时x的值是9
x = x++;
//理论上此时x的值是9才对啊,可是此时打印出来x的值实际上是8,我在这里疑惑不解?
System.out.println("x = " + x);
/*理论上此时x的值是9,x先自加1,加完后x的值是10,然后再把10赋值给x,此时x的值理论上来说应
* 该是10才对,可是此时打印出来x的值实际上是9,我在这里疑惑不解?
*/
x = ++x;
System.out.println("x = " + x);
System.out.println("-----------------");
int y = 8;
//y的初始值是8,先把y赋值给z,此时z的值是8,y再自加1,此时内存中y的值是9
int z = y++;
System.out.println("y = " + y + " z = " + z);
//此时内存中y的值是9,y先自加1,此时内存中y的值是10,再把y赋值给z,此时z的值是10
z = ++y;
System.out.println("y = " + y + " z = " + z);
System.out.println("-----------------");
int a = 66;
//先打印a的值,a再自加上1
System.out.println(a++);
System.out.println(a);
System.out.println("-----------------");
int b = 88;
//b先自加1,再打印b的值
System.out.println(++b);
System.out.println(b);
System.out.println("-----------------");
int number = 23;
number = number + 2;
System.out.println("number = " + number);
System.out.println("-----------------");
int age = 23;
age = age++;
//不明白为什么age打印出来还是23,难道不应该是24吗?谁能解释下?
System.out.println("age = " + age);
System.out.println("-----------------");
int salary = 12666;
salary = ++salary;
//打印出来是12667,谁能解释下?
System.out.println("salary = " + salary);
}
}