这个后增操作什么时候发生?

huangapple go评论72阅读模式
英文:

When does this post-incrementation happen?

问题

我面前有这段 Java 代码:

private static int[] generateSparseArray(int value) {
    int[] array = new int[getBound(value)];
    for (int j = 1; j < value; j++) {
        do {
            array[j++] = value;
            if ((value++ > 3 || value < 4) && (j > 0)) {
                ++j;
                break; 
            }
        } while (value < 4); 
    }
    return array; 
}

private static int getBound(int value) { /** ... */ }

我不确定这段代码是否会在检查布尔表达式的这部分后递增 "value",还是会在整个 if 语句的布尔表达式之后递增。

你们认为呢?

英文:

I have this piece of Java code in front of me:

private static int[] generateSparseArray(int value) {
    int[] array = new int[getBound(value)];
    for (int j = 1; j &lt; value; j++) {
        do {
            array[j++] = value;
            if ((value++ &gt; 3 || value &lt; 4) &amp;&amp; (j &gt; 0)) {
                ++j;
                break; 
            }
        } while (value &lt; 4); 
    }
    return array; 
}

private static int getBound(int value) { /** ... */ }

I am not sure if this will increment "value" after checking specifically this piece of the boolean expression or if it will increment after the entire boolean expression of the if-statement.

What do you guys think?

答案1

得分: 2

The post-increment is done immediately after evaluation of value, not after the evaluation of the whole if condition.

Look at (value++ > 3 || value < 4) and with value being 3:

  • value is evaluated and is 3, so the first part of the condition is false
  • immediately afterwards value is incremented to 4
  • Since the first part of the OR expression was false, the second part is evaluated and not short-circuited.
  • Now value is already 4 and the second part of the condition is also false

You can verify this by looking at a stripped down version.

public static void main(String[] args) {
    int a = 3;
    if((a++ > 3 || a < 4) && verify(a)) {

    }
}

static boolean verify(int a) {
    System.out.println(a);
    return true;
}

The method verify is never called and nothing is printed, because (a++ > 3 || a < 4) is false and short-circuiting takes place.

英文:

The post-increment is done immediately after evaluation of value, not after the evaluation of the whole if condition.

Look at (value++ &gt; 3 || value &lt; 4) and with value being 3:

  • value is evaluated and is 3, so the first part of the condition is false
  • immediately afterwards value is incremented to 4
  • Since the first part of the OR expression was false, the second part is evaluated and not short-circuited.
  • Now value is already 4 and the second part of the condition is also false

You can verify this by looking at a stripped down version.

public static void main(String[] args) {
    int a = 3;
    if((a++ &gt; 3 || a &lt; 4) &amp;&amp; verify(a)) {

    }
}

static boolean verify(int a) {
    System.out.println(a);
    return true;
}

The method verify is never called and nothing is printed, because (a++ &gt; 3 || a &lt; 4) is false and short-circuiting takes place.

答案2

得分: 0

这与是否在 if 语句中并没有真正的关系;它只是表达式求值。

这个操作在 Java 语言规范 中明确地进行了说明。

二元操作符的左操作数在右操作数的任何部分被求值之前似乎会被完全求值。

英文:

It's not really anything to do with being in an if statement; it is simply expression evaluation.

The operation is spelled out explicitly in the Java Lanaguage Specification.

> The left-hand operand of a binary operator appears to be fully evaluated
> before any part of the right-hand operand is evaluated.

huangapple
  • 本文由 发表于 2020年10月10日 20:08:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/64293241.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定