Java 8/Javascript (Nashorn) long interoperatiblity

2019-07-11 03:51发布

The following Javascript code executed in Java 8 (Nashorn) does not behave as expected :

if( a != b )
{
  do_sth();
}

a and b are long values coming from Java object (e.g., 1023948, 1023949). For example, when a = 1023949 and b = 1023949, a != b is true.

Note that the following code works fine:

if( (a+0) != (b+0) )
{
  do_sth();
}

I know about long precision issue (as Javascript numbers are 64 doubles) but I was expecting that "small" long values should work.

Any input is appreciated. Thx.

1条回答
放我归山
2楼-- · 2019-07-11 04:36

I guess Nashorn passes the long values as JS objects to the JS side and thus the comparison returns wrong even though the values are same.

You can check with typeof a and b on JS side.

查看更多
登录 后发表回答