使用 Java 三元运算符或数学函数时,返回值是否始终是最新的?

When using Java ternary operator or Math functions, is the returned value always up to date?

当使用 Java 三元运算符或数学函数时,返回值是否始终是最新的,或者每当我需要更新值时是否必须重新评估语句?

正在考虑:

a = myBoolean? 1: 2;
a = Math.min(x, y);

"a" 的值是否会像在...

中那样动态变化
a = x + y;

...什么时候 "x" 或 "y" 改变?

评估变量(表达式)的当前值作为参数传递给方法。当变量(表达式)用作运算符操作数时也会发生同样的情况1.

稍后更改原始变量的值 - 在代码执行后 - 对[已经完成]的计算没有影响。原始表达式已作为输入进行评估(至 values),并将生成的 value 分配给变量。

这也称为 eager evaluation:

In eager evaluation, an expression is evaluated as soon as it is bound to a variable [or supplied to a method or used with an operator] ..

.. Imperative programming languages [like Java], where the order of execution is implicitly defined by the source code organization, almost always use eager evaluation.


1 ternary/conditional (?:)、逻辑与 (&&) 和逻辑或 (||) 运算符也适用于 short-circuit evaluation.