public class A {
public static int a = 0;
public static void main(String[] args) {
for(int i = 0;i < 10;i++){
a = a++;
System.out.println(a);
}
System.out.println(a);
}
}
最后的结果:a = 0;
中间打印出的a也全部是0;
为什么??++到底加到哪里去了????

解决方案 »

  1.   

    a++;是先赋值给a后再加1
    那在下一次循环的时候a就应该加了1了  
    怎么会还是0呢?
      

  2.   

    a = a++;
    ..................a++;是先赋值给左边的a后,  再自身a才加1, 明了没。
      

  3.   

    是先赋值给左边的a后,  再自身a才加1, 明了没。
    这个我知道,第一次循环a = 0;
    然后a 附值 ,a 加1 
    这时第2次循环,a就应该是1了吧
      

  4.   

    以前用JBuilder 运行是0
    调试结果是23  
    茫然 
    现在用Eclipse
      

  5.   

    大哥,你赋值是赋给了左边的a值,是public static int a的值所以全部循环也是0啊. (a++起不了什么作用)在eclipse答案也是:
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
      

  6.   

    逻辑错误你的过程是:先用a来保存a++的值(值为0),然后a+1,可接着又把a赋回初始值,正确的写法是:a++;a=a++;等价于:
    int temp=a;
    a=a+1;
    a=temp由此我们得到一个教训:不要在单个的表达式中对相同的变量赋值超过一次
      

  7.   

    a=a++;等价于:
    int temp=a;
    a=a+1;
    a=temp有点意思   最后那个a = temp 从哪出来的???
      

  8.   

    似乎
    a = b + (b = a) * 0;
    这句话能互换a,b两int值的道理能解释这个问题。
    吃饭先,回来继续看。
      

  9.   

    所以按照我的理解,上面的
    a = a ++;
    其实执行顺序是:
    temp = a; // 也就是temp = 0;
    a ++; // a此时 = 1;
    a = temp; //将temp值赋给a。a又恢复为0;
      

  10.   

    waiting20052005() ( ) 信誉:100  2006-08-10 18:25:00  得分: 0  
     
     
       a=a++;等价于:
    int temp=a;
    a=a+1;
    a=temp有点意思   最后那个a = temp 从哪出来的???
      
     
      

  11.   

    上次好象也碰到过类似的题,过程应该是这样的:
    1、计算右边表达式a++的值,值为0;(因为它是先计算,在自增)
    2、右边对i进行自增操作;
    3、将右边的计算结果赋给左边的a,当然用的是1中的值,因为1中计算的是右边表达式的值;
       这样a的值就一直都是0了。
      

  12.   

    1。a=0;
    2.a=a//a的值还是0;
    3.下一步就成了a=++;当然如果写出这个会是语法错误。但其实a=a++就是这样做的,所以它的值还是为原来的初始值全部打印0,因为根本就没有+1;
      

  13.   

    上面的是在eclipse里面编译器的操作过程,在c++用答案就不是这样了,c++中编译的答案是:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    10因为在c++中是先赋值再自增。
      

  14.   

    这个问题,早就讨论过
    jfreshman 的blog中也有,他的名字忘了,差不多就是叫jFreshManhttp://community.csdn.net/Expert/topic/4716/4716358.xml?temp=.468075http://community.csdn.net/Expert/topic/4725/4725070.xml?temp=.5039179
    参见上面帖子
      

  15.   

    对于a++表达式,相应操作是a自加1,a的原值参与表达式以外的操作a=a++; 相当于存储器先记住a的原值,然后a=a+1,最后再把原值还给a,所以a是不变的
      

  16.   

    welshem(天堂客)
    说的应该是正确的。
      

  17.   

    在C++Builder6里面运行:#include<stdio.h>
    #include<stdlib.h>
    int main()
    {
      int a = 0;
      int c = 1;
      for(c=0;c<10;c++)
       {
         a = a++;
         printf("a=%d",a);
       }
      return 0;
    }结果:
    a=1a=2a=3a=4a=5a=6a=7a=8a=9a=10没有上面所说的现象啊
      

  18.   

    应该是用了“public static int a = 0;”
    int a  成了static 产生的效果吧
      

  19.   

    改了一下程序:
    #include<stdio.h>
    #include<stdlib.h>
    int main()
    {
      int static a = 0;//改过的static
      int c = 1;
      for(c=0;c<10;c++)
       {
         a = a++;
         printf("a=%d",a);
       }
      return 0;
    }结果也没有变化
      

  20.   

    x++:
    ++是作为operator,
    返回"结果"为x的值,x本身加一x++
    {
    temp=x;
    x+=1;
    return temp;
    }++x
    {
    x+=1;
    return x;
    }
    因此,如果只是帮x加一
    用++x比x++好,会少用一个变量temp ,这在for循环中常见
    for(int i=0;i<12;++i) //better
    for(int i=0;i<12;i++)因此
    a=a++;
    无论循环多少次,a的值总是被赋予最开始的值
      

  21.   

    对于
    int a=0;
    a=a++;反编译以下为:
     0:   iconst_0
     1:   istore_1
     2:   iload_1
     3:   iinc    1, 1
     6:   istore_1大家可以看到了,先是把a的值0压入栈,然后再对a加1,再把栈里的值0保存给a
    所以a其实是先加了1的,只不过又赋回原来的值了。
      

  22.   

    不知道是我学艺不精还是你们把问题复杂化了........我觉得有了static就是a += 1000000........最后结果也是0阿
      

  23.   

    可以用javap -c ***.class 查看编译过程。
      

  24.   

    Compiled from "TestA.java"
    public class TestA extends java.lang.Object{
    public static int a;static {};
      Code:
       0:   iconst_0
       1:   putstatic       #11; //Field a:I
       4:   returnpublic TestA();
      Code:
       0:   aload_0
       1:   invokespecial   #16; //Method java/lang/Object."<init>":()V
       4:   returnpublic static void main(java.lang.String[]);
      Code:
       0:   iconst_0
       1:   istore_1
       2:   goto    29
       5:   getstatic       #11; //Field a:I
       8:   dup
       9:   iconst_1
       10:  iadd
       11:  putstatic       #11; //Field a:I
       14:  putstatic       #11; //Field a:I
       17:  getstatic       #26; //Field java/lang/System.out:Ljava/io/PrintStream;
       20:  getstatic       #11; //Field a:I
       23:  invokevirtual   #32; //Method java/io/PrintStream.println:(I)V
       26:  iinc    1, 1
       29:  iload_1
       30:  bipush  10
       32:  if_icmplt       5
       35:  getstatic       #26; //Field java/lang/System.out:Ljava/io/PrintStream;
       38:  getstatic       #11; //Field a:I
       41:  invokevirtual   #32; //Method java/io/PrintStream.println:(I)V
       44:  return}
      

  25.   

    在builder中!!如果把a=a++换成 a++的话
    那么输出结果是 
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    10
      

  26.   

    i++ 你可以理解为一个函数 int inc(int i)int inc(int i) {
        i = i + 1;
        return (i - 1);
    }这样,返回的结果就是传入的i的值,但实际上i的值确实是发生了改变
      

  27.   

    为什么要改程序?这道题测试的预期效果这一改不改没了?a=a++;
    ----------------------
    将上面的分解下来,先把a++这个运算赋给a,这个时候是把a在++之前的值赋给a
      

  28.   

    a=++a;就行了~~首先要明白 a++ 和++a的区别~~ 马上得出答案
      

  29.   

    有了这一句:public static int a = 0;
    不管你怎么加都是0
    根据java规范,java静态变量永远等于第一次赋的值
      

  30.   

    leebx(浩如海):看好了,这是成员变量...是在成员函数里调用...
      

  31.   

    楼上的    
    根据java规范,java静态变量永远等于第一次赋的值
    那下面这个程序里的 sum 的值怎么会变的???
    public class CMain {
    /**
     * @param args
     * 递归算法求1-50的和
     */
    public static int sum = 0;//存储结果
    public static int Add(int i){
    sum += i;
    if(i == 1)
    return sum;
    else
    return Add(i - 1);
    }
    public static void main(String[] args) {
    // TODO Auto-generated method stub
    int i = 50;
    int s = Add(i);
    System.out.println(s);
    }
    }
      

  32.   

    真的是编译器有关系吗?import java.io.*;public class test {
    public static int a = 0;
    public static int b = 0;
    public static int c = 0;
    public static int d = 0;
    public static void main(String[] args){

             b = a++;
    System.out.println(a);
    System.out.println(b);
    b = b++;
    System.out.println(b);

    d = ++c;
    System.out.println(c);
    System.out.println(d);

    }
    }=========
    1
    0
    0
    1
    1
    ==========
    理解好a++,++a才是正道啊~~~~~~~~~~
      

  33.   

    ok, 'sum+=i'; is equivalent to 'sum=sum + i';
      

  34.   

    If you define a field as static, then there is only one such field per class. In contrast, each object has its own copy of all instance fields. For example, let's suppose we want to assign a unique identification number to each employee. We add an instance field id and a static field nextId to the Employee class:class Employee
    {
       . . .
       private int id;
       private static int nextId = 1;
    }Every employee object now has its own id field, but there is only one nextId field that is shared among all instances of the class. 
      

  35.   

    In most object-oriented programming languages, static fields are called class fields. The term "static" is a meaningless holdover from C++.
      

  36.   

    please compair two application
    ----------------
    public class A {
        public static int a= 0;
    /**
     * @param args
     */
    public static void main(String[] args) {
    // TODO Auto-generated method stub
            for (int i=0; i<10; i++){
             a=++a;
             System.out.println(a);
            }
            System.out.print("k");
            System.out.println(a);
    }
        
    }
    -----------------------
    public class A {
        public static int a= 0;
    /**
     * @param args
     */
    public static void main(String[] args) {
    // TODO Auto-generated method stub
            for (int i=0; i<10; i++){
             a=a++;
             System.out.println(a);
            }
            System.out.print("k");
            System.out.println(a);
    }
        
    }
    ---------------------
    run it in the eclipse,
    -----------------------
      

  37.   

    java的编译器搞的鬼!在遇到++和--操作符的时候会重新为原变量分配一块内存空间,以存放原始的值,而在完成了赋值运算之后,就将这块内存释放掉。由于i的原始值存放在后开辟的内存中,这样i=i++后,由于是先赋值,i就会得到i的原始值(存放在新内存中的数椐),而原来内存位置的i自加后只保留在原来的位置,由于此时i指向已经是新开辟出来的内存地址,所以i的值没有发送变化!
    换句话说,
    while(true){
       i=i++;
    }
    永远执行i的值恒等于i的初始值,即使不是0也一样!下面我把过程写一下i=0;//假设此时内存地址为0x12345678
    i=i++;//系统新开内存地址0x99999999,存放i原始值0,然后0x12345678的存放数据+1操作
          //此时0x12345678=1,0x99999999=0,但是上一步是先给值,所以i的内存地址是0x99999999=0;所以i=0,但是,如果是
    i=0;
    i++;
    此时i=1,因为0x99999999处新开辟的内存地址没有给任何引用,所以被丢弃了!i继续使用0x12345678处值
      

  38.   

    hugomyj(myj) ( ) 信誉:100  2006-08-11 10:58:00  得分: 0  
     
     
       为什么不给我分,让我研究了半天!
      
     
    呵呵和  不好意思   
    人太多了