Java 8/Javascript (Nashorn) long interoperatiblity

故事扮演 提交于 2019-12-11 03:07:08

问题


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:


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.



来源:https://stackoverflow.com/questions/41281261/java-8-javascript-nashorn-long-interoperatiblity

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!