package org.asheng.test; /** * "i++" "i--" 先赋值再运算 * "++i" "--i" 先运算再赋值 */ public class PrePostTest { public static void main(String[] args) { int i = 3; i++; System.out.println(i); // "4" ++i; System.out.println(i); // "5" System.out.println(++i); // "6" System.out.println(i++); // "6" System.out.println(i); // "7" int a = 0, b; for (b = 0 ; b < 10; b++) { a = a++; System.out.println("a = " + a); } int x = 0, y; for (y = 0 ; y < 10; y++) { x = ++x; System.out.println("x = " + x); } } }
command prompt"> javac -sourcepath src/main/java/ -d classes/ src/main/java/org/asheng/test/PrePostTest.java
java -classpath classes/ org.asheng.test.PrePostTest 4 5 6 6 7 a = 0 a = 0 a = 0 a = 0 a = 0 a = 0 a = 0 a = 0 a = 0 a = 0 x = 1 x = 2 x = 3 x = 4 x = 5 x = 6 x = 7 x = 8 x = 9 x = 10