话不多说,先上demo
package test;
/**
* Created by Vitelon on 2017/3/10.
*/
public class Demo1 {
public static void main(String[] args) {
int a = 4;
int b = 4;
int c = 4;
int d = 4;
a++;
++b;
c--;
--d;
System.out.println("a = "+a); //5
System.out.println("b = "+b); //5
System.out.println("c = "+c); //3
System.out.println("d = "+d); //3
//++--运算符单独使用,在前在后都一样的结果
}
}
package test;
/**
* Created by Vitelon on 2017/3/10.
*/
public class Demo1 {
public static void main(String[] args) {
int a = 4;
int b = 4;
int c = 4;
int d = 4;
int i,j,k,l;
i = a++;
j = ++b;
k = c--;
l = --d;
System.out.println("i = "+ i +",a ="+ a); //i=4, a=5
System.out.println("j = "+ j +",b ="+ b); //j=5, b=5
System.out.println("k = "+ k +",c ="+ c); //k=4, c=3
System.out.println("l = "+ l +",d ="+ d); //l=3, d=3
//++--运算符,在前时,是先增减再赋值;在后时,是先赋值再增减。
}
}
总结:算数运算符++(--),当单独使用时,在前在后都是一样的结果;当结合赋值运算时,在前时,是先增减再赋值;再后时,是先赋值再增减。
简单的总结,记录下来,有时忘记了可以回过头来看一下,写得不好请见谅。