英文:
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 < 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) { /** ... */ }
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 is3
, so the first part of the condition isfalse
- immediately afterwards
value
is incremented to4
- Since the first part of the OR expression was false, the second part is evaluated and not short-circuited.
- Now
value
is already4
and the second part of the condition is alsofalse
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++ > 3 || value < 4)
and with value
being 3
:
value
is evaluated and is3
, so the first part of the condition isfalse
- immediately afterwards
value
is incremented to4
- Since the first part of the OR expression was false, the second part is evaluated and not short-circuited.
- Now
value
is already4
and the second part of the condition is alsofalse
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.
答案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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论