Nashorn:比较 BigDecimal 与数字的严格相等
Nashorn: Strict equality comparing BigDecimal with number
在 Oracle 的 JDK 1.8.0_121 中,在 Nashorn(JDK 中嵌入的 JavaScript 引擎中,new BigDecimal(1.0) === 1
是 false
,new BigDecimal(1.0) == 1
是 true
:
使用 JDK 1.8.0_121 的 jjs
(Nashorn REPL):
jjs> var BigDecimal = Java.type("java.math.BigDecimal")
jjs> var bd = new BigDecimal(1.0)
jjs> bd
1
jjs> bd === 1.0
false
jjs> bd == 1.0
true
使用 JDK 1.8.0_74 的 jjs
:
jjs> var BigDecimal = Java.type("java.math.BigDecimal")
jjs> var bd = new BigDecimal(1.0)
jjs> bd
1
jjs> bd === 1.0
true
jjs> bd == 1.0
true
这是 Nashorn 中对平等的严格规则的已知收紧吗? Nashorn 中是否有明确的 ===
严格相等运算符规范可以解释这种行为并希望行为发生变化?
或者这是 JDK 中的倒退?
这是 JDK 1.8.0_101 及更高版本中的有意更改,记录在 JDK-8143896. The handling of strict equality must be intentional, since it is called out with a test case that covers BigDecimal being compared to an integer.
中
JDK 发行说明中没有提到这一点,但可以确认这是行为上的有意更改。
在 Oracle 的 JDK 1.8.0_121 中,在 Nashorn(JDK 中嵌入的 JavaScript 引擎中,new BigDecimal(1.0) === 1
是 false
,new BigDecimal(1.0) == 1
是 true
:
使用 JDK 1.8.0_121 的 jjs
(Nashorn REPL):
jjs> var BigDecimal = Java.type("java.math.BigDecimal")
jjs> var bd = new BigDecimal(1.0)
jjs> bd
1
jjs> bd === 1.0
false
jjs> bd == 1.0
true
使用 JDK 1.8.0_74 的 jjs
:
jjs> var BigDecimal = Java.type("java.math.BigDecimal")
jjs> var bd = new BigDecimal(1.0)
jjs> bd
1
jjs> bd === 1.0
true
jjs> bd == 1.0
true
这是 Nashorn 中对平等的严格规则的已知收紧吗? Nashorn 中是否有明确的 ===
严格相等运算符规范可以解释这种行为并希望行为发生变化?
或者这是 JDK 中的倒退?
这是 JDK 1.8.0_101 及更高版本中的有意更改,记录在 JDK-8143896. The handling of strict equality must be intentional, since it is called out with a test case that covers BigDecimal being compared to an integer.
中JDK 发行说明中没有提到这一点,但可以确认这是行为上的有意更改。