Java “i++”和“++i”与"i--"和"--i"区别_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > Java “i++”和“++i”与"i--"和"--i"区别

Java “i++”和“++i”与"i--"和"--i"区别

 2011/10/12 9:15:12  asheng  http://asheng.iteye.com  我要评论(0)
  • 摘要:packageorg.asheng.test;/***"i++""i--"先赋值再运算*"++i""--i"先运算再赋值*/publicclassPrePostTest{publicstaticvoidmain(String[]args){inti=3;i++;System.out.println(i);//"4"++i;System.out.println(i);//"5"System.out.println(++i);//"6"System.out.println(i++)
  • 标签:区别 Java
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
发表评论
用户名: 匿名