Java 8 / Javascript (Nashorn) long interoperable

The following Javascript code executed in Java 8 (Nashorn) does not work properly:

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

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

Please note that the following code works fine:

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

I know about the problem with long precision (since the Javascript numbers are 64 doubles), but I expected the "small" long values ​​to work.

Any input is appreciated. thanks.

+2
source share
1 answer

I think Nashorn passes long values ​​as JS objects to the JS side, and thus the comparison returns an error even if the values ​​are the same.

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

0
source

Source: https://habr.com/ru/post/1269498/


All Articles