中间缓存变量机制

</pre><p>1)例题为:</p><pre name="code" class="html">public class Test1
{
    public static void main(String[] args)
 {
    int j=0;
    for(int i=0;i<100;i++)
    {
       j=j++;
     }
     System.out.println(j);
 } 
}


Java使用了中间缓存变量机制,j++可以看作:

开始j=0;

        j++      temp=j=0     j=1

最后   j=temp=0

本题中这个过程循环了100次

2)例题为:

public class Test2
{
    public static void main(String[] args)
 {
    int j=0;
    j= ++j + j++ + j++ +j++;
     System.out.println(j);
 } 
}
此题中j多次的加1,或者自加,因此四个j表达式的值与本身j在不同式子中的值是不统一的。

     其中: ++j     temp1=j+1=1   j =1  ;

                  j++     temp2=j=1        j=2   ;

                  j++     temp3=j=2        j=3   ;

                  j++     temp4=j=3        j=4   ;

                 最后,  j=temp1+temp2+temp3+temp4=1+1+2+3=7。




版权声明:本文为cocky123原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。