Java三元运算符和后缀/前缀操作

时间:2021-08-11 22:29:52

I would like to understand behavior of that codes:

我想了解这些代码的行为:

If we use prefix increment:

如果我们使用前缀increment:

public class Ternary{
    public static void main(String[] args){
       int k=0;
       for(int i=0; i<10; i++){
          k = (k < 3) ? ++k : 0;
          System.out.println(k);
       }
    }
}

Output is: 1 2 3 0 1 2 3 0 1 2

输出是:1 2 3 0 1 2 3 0 1 2。

But if use postfix increment:

但如果使用后缀增量:

public class Ternary{
    public static void main(String[] args){
       int k=0;
       for(int i=0; i<10; i++){
          k = (k < 3) ? k++ : 0;
          System.out.println(k);
       }
    }
}

Output is: 0 0 0 0 0 0 0 0 0 0

输出是:0 0 0 0 0 0 0 0 0 0 0 0 0

What is the difference and why we don't have the same output ?

有什么区别,为什么输出不一样?

3 个解决方案

#1


5  

++k

Is interpreted first and then the value is used

首先解释,然后使用值

k++

Is first used and after that the value is incremented

是第一次使用,然后值递增

So in the second case you are saying

在第二种情况下,你说

k = k++;

And that wont effectively change the value of k

这并不能有效地改变k的值

Personally i would prefer something like this:

我个人更喜欢这样:

 k = (k < 3) ? k + 1 : 0;

Because you don't really want to increment k and then give k that value, that wouldn't make much sense

因为你并不想增加k然后给k那个值,这没有多大意义

Perhaps see here a longer explanation

也许在这里可以看到一个更长的解释

#2


2  

k = (k < 3) ? ++k : 0;

This means:

这意味着:

1) increment k

1)增加k

2) evaluate (k < 3) ? (new k) : 0;

2)评价(k < 3) ?(新k):0;

3) save result of step 2) in k

3)将步骤2的结果保存在k中

k = (k < 3) ? k++ : 0;

This means:

这意味着:

1) evaluate (k < 3) ? (old k) : 0;

1)评价(k < 3) ?(老k):0;

2) increment k;

2)增量k;

3) save result of step 1) in k (which overwrites the value of step 2)

3)将步骤1的结果保存在k中(它覆盖了步骤2的值)

What you are trying to achieve is better done without assigning the result of the RHS to k.

您要实现的是最好不要将RHS的结果赋给k。

#3


0  

k++; simply means k = k + 1;

k + +;简单地说k = k + 1;

++k means increment and then assign the new value to k.

+k表示增量,然后将新值赋给k。

#1


5  

++k

Is interpreted first and then the value is used

首先解释,然后使用值

k++

Is first used and after that the value is incremented

是第一次使用,然后值递增

So in the second case you are saying

在第二种情况下,你说

k = k++;

And that wont effectively change the value of k

这并不能有效地改变k的值

Personally i would prefer something like this:

我个人更喜欢这样:

 k = (k < 3) ? k + 1 : 0;

Because you don't really want to increment k and then give k that value, that wouldn't make much sense

因为你并不想增加k然后给k那个值,这没有多大意义

Perhaps see here a longer explanation

也许在这里可以看到一个更长的解释

#2


2  

k = (k < 3) ? ++k : 0;

This means:

这意味着:

1) increment k

1)增加k

2) evaluate (k < 3) ? (new k) : 0;

2)评价(k < 3) ?(新k):0;

3) save result of step 2) in k

3)将步骤2的结果保存在k中

k = (k < 3) ? k++ : 0;

This means:

这意味着:

1) evaluate (k < 3) ? (old k) : 0;

1)评价(k < 3) ?(老k):0;

2) increment k;

2)增量k;

3) save result of step 1) in k (which overwrites the value of step 2)

3)将步骤1的结果保存在k中(它覆盖了步骤2的值)

What you are trying to achieve is better done without assigning the result of the RHS to k.

您要实现的是最好不要将RHS的结果赋给k。

#3


0  

k++; simply means k = k + 1;

k + +;简单地说k = k + 1;

++k means increment and then assign the new value to k.

+k表示增量,然后将新值赋给k。