后置和前置增量运算符

发布于 2024-12-22 18:23:11 字数 605 浏览 2 评论 0原文

当我运行以下示例时,我得到输出 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

When i run the following example i get the output 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?
        } 
    } 

I don't understand why the output is 0,2,1 and not 0,2,2

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

提笔落墨 2024-12-29 18:23:11

如果我们展开 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

我想主要步骤可以像上面那样总结。

If we expand i = i++ + f1(i) statement, we get something like following

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 guess main steps can be summarized like above.

舞袖。长 2024-12-29 18:23:11
 i = i++ + f1(i);  

i++ 表示 i 现在是 2。调用 f1(i) 打印 2 但返回 0,因此 i=2j=0

在此 < code>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; 

另一个这样的示例:

 i = i++ + f1(i);  

i++ means i is now 2. The call f1(i) prints 2 but returns 0 so i=2 and j=0

before this i = 1 , now imagine f1() called and replaced with 0

so

i = i++ + 0;

now it would be

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

In Simpler words (from here @ Piotr )

"i = i++" roughly translates to

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

Another such Example :

烈酒灼喉 2024-12-29 18:23:11

从这个例子可以理解解决方案

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,您正在通过赋值运算符覆盖它。

The solution can be understood from this example

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

Hence from this line,

i = i++ + f1(i); 

Your i is still 1 and obviously the function is going to return 0. Which is again stored in i and hence the value 1. Instead of the updated value of i being stored in i, you are overriding it by the assignment operator.

橙幽之幻 2024-12-29 18:23:11

在后增量运算符中,操作数的值将在使用后增加。
示例

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

第一个 k(值 = 1)被赋值为 l,之后 k 值将增加

类似的事情发生在下面的行中

i = i++ + f1(i);

In Post increment operator value will of operand will increase after use.
Example

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

First k (value = 1) is assigned with l after that the k value will increase

Similarly thing happens in the following line

i = i++ + f1(i);
成熟的代价 2024-12-29 18:23:11

预自增是指:变量加一,返回增量后的值;
后自增——先返回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;

Pre increment means: add one to variable and return incremented value;
Post increment - first return i, then increment it;

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;
故笙诉离歌 2024-12-29 18:23:11

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

这里对方程 i++ + f1(i) 求值有一些解释

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

To get In-depth you need to see Expression and its Evaluation Order

Here's little explanation about equation i++ + f1(i) evaluation

In Equation Compiler get "i" which is Equals to 1 and put it on stack as first operand then increments "i", so its value would be 2, and calculates second operand by calling function which would be 0 and at the time of operation (+) execution operands would be 1 and 0.

月寒剑心 2024-12-29 18:23:11

希望这个解释可能有一些帮助:

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;

那么两者的意思是一样的。

问候

Hope this explaination might be of some help :

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.   

Do try this

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)'

So in short during post increment, expression is first solved and then value is incremented. But in pre increment, value is first incremented and then expression is solved.

But if you write only

i++;
++i;

Then both means the same thing.

Regards

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文