繁体   English   中英

Post和Pre增量运算符

[英]Post and Pre increment operators

当我运行以下示例时,我得到输出0,2,1

class ZiggyTest2{

        static int f1(int i) {  
            System.out.print(i + ",");  
            return 0;  
        } 

        public static void main(String[] args) {  
            int i = 0;  
            int j = 0;  
            j = i++;   //After this statement j=0 i=1
            j = j + f1(j);  //After this statement j=0 i=1
            i = i++ + f1(i);  //i++ means i is now 2. The call f1(2) prints 2 but returns 0 so i=2 and j=0
            System.out.println(i);  //prints 2?
        } 
    } 

我不明白为什么输出是0,2,1而不是0,2,2

如果我们扩展i = i++ + f1(i)语句,我们得到类似下面的内容

save the value of i in a temp variable, say temp1 // temp1 is 1
increment i by one (i++)                          // i gets the value 2
execute f1(i), save return value in, say temp2    // temp2 is 0, print 2
assign temp1 + temp2 to i                         // i becomes 1 again

我想主要步骤可以如上所述进行总结。

 i = i++ + f1(i);  

i++意味着i现在是2 调用f1(i)打印2但返回0,因此i=2j=0

在此i = 1之前,现在想象f1()调用并替换为0

所以

i = i++ + 0;

现在它会

i = 1 + 0 // then it will increment i to 2 and then (1 +0) would be assigned back to `i`

用简单的话说(从这里 @Piotr)

“i = i ++”粗略地翻译成

int oldValue = i; 
i = i + 1;
i = oldValue; 

又如这样的例子:

从该示例可以理解该解决方案

public static void main(String[] args) {
    int i = 0;
    i = i++;
    System.out.println("i is" + i);
}
/* The output is "i is 0" */

因此,从这条线,

i = i++ + f1(i); 

你的i仍然是1,显然函数将返回0.它再次存储在i中,因此值为1.而不是i的更新值存储在i中,您将通过赋值运算符覆盖它。

预增量意味着:向变量添加一个并返回递增值; 后增量 - 首先返回i,然后增加它;

int i, j, k;
i = 0; // 0
j = i++; // return i , then increment i
// j = 0; i = 1;
k = ++i; // first increment and return i
//k = 2; i = 2;

// now
++j == --k == --i // would be true => 1==1==1;
// but , using post increment would 
// j++ == k-- == i-- // false because => 0 == 2 == 2;
// but after that statement j will be 1, k = 1, i = 1;

希望这种解释可能会有所帮助:

j = i++; // Here since i is post incremented, so first i being 0 is assigned to j
         // and after that assignment , i is incremented by 1 so i = 1 and j = 0.

i = i++ + f1(i); // here again since i is post incremented, it means, the initial value 
                 // of i i.e. 1 as in step shown above is used first to solve the 
                 // expression i = i(which is 1) + f1(1)**Since i is 1**
                 // after this step the value of i is incremented. so i now becomes 2
                 // which gets displayed in your last System.out.println(i) statement.   

试试这个

i = ++i + f1(i); // here i will be first inremented and then that value will be used 
                 // to solve the expression i = i + f1(i)'

因此,在后期增量期间,首先解决表达式,然后递增值。 但是在预增量中,首先递增值,然后求解表达式。

但如果你只写

i++;
++i;

然后两者意味着同样的事情。

问候

在Post增量运算符中,操作数将在使用后增加。

int k =1;
        int l = k++;
        System.out.println("...k..."+k+"...l.."+l);

首先k(值= 1)被赋予l,之后k值将增加

同样的事情发生在以下行

i = i++ + f1(i);

要深入了解,您需要查看表达式及其评估顺序

这里关于方程式i ++ + f1(i)评估的一点解释

在公式编译器中得到“i”等于1并将其作为第一个操作数放在堆栈上然后递增 “i” ,因此其值为2 ,并通过调用函数来计算第二个操作数,该函数将为0并且在操作时(+)执行操作数将为10

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM