Java 8/Javascript (Nashorn) long interoperatiblity

805 Views Asked by At

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

There are 1 best solutions below

0
On

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.