java中i++、++i、i--、--i的含义以及经典例题亲测整理

1、i++先赋值后自加,i--同理先赋值后自减

int a1= 2;
//先赋值后自加    b1=a1;  a1=a1+1;
int b1 = a1++;
//b1=2    a1=3
System.out.println(b1);
System.out.println(a1);

 2、++i先自加后赋值,--i同理先自减后赋值

int a1= 2;
//先自加后赋值    a1=a1+1;    b2=a1;
int b2 = ++a1;
//b2=a1=3
System.out.println(b2);
System.out.println(a1);

3、来几个例题试试

int a= 2;
//b=9       (a++) = 2     a=3
int b = (3 * a++) + a;
System.out.println(b);
System.out.println(a);
int a = 2;
//b=8    a=2     (a++) = 2      a=3
int b = a + (3 * a++);
System.out.println(b);
System.out.println(a);
int i = 1;
int j = 1;
//int k = (i++) + (++i) + (++j) + (j++);
int k = i++ + ++i+ ++j+j++;
//k=8   (i++)=1   i=2  (++i)=3  i=3     (++j)=2     j=2     (j++)=2     j=3
//k = 1 + 3 + 2 + 2     = 8
System.out.println(k);
//j=3
System.out.println(j);
//i=3
System.out.println(i);
int a = 0;
int b = 0;
//运算过程为 _a=0     a=_a+1=1     a=_a=0    其中_a为寄存器
a = a++;
//a=0
System.out.println(a);
//运算过程为 _a=0     a=_a+1=1     b=_a=0
b = a++;
//a=1   b=0
System.out.println(a);
System.out.println(b);
int a = 0;
//b = (a++) + a
//b=1   (a++)=0     a=1
int b = a+++a;
//b=1   a=1
System.out.println(a);
System.out.println(b);
int a = 0;
//b = (a++) + (a++)
//b=1   (a++)=0     a=1     (a++)=1     a=2
int b = a+++a++;
//b=1   a=2
System.out.println(a);
System.out.println(b);

 

你可能感兴趣的:(计算机基础,java)