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);