2

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.

Marc Polizzi
  • 9,275
  • 3
  • 36
  • 61
  • I might be missing something but you expect `a == b` to be true when `a` and `b` are `Long` objects with the same `long` value? I don't know what Nashorn is doing here but if in that case `a != b` then it's probably the Java logic being applied, i.e. `new Long(12345) != new Long(12345)` (those are equal but not the same instances). – Thomas Dec 22 '16 at 10:46
  • You might be right as the issue does not appears with values -128:127. Looks like autoboxing is used... – Marc Polizzi Dec 22 '16 at 11:10
  • See https://bugs.openjdk.java.net/browse/JDK-8161665 and http://mail.openjdk.java.net/pipermail/nashorn-dev/2016-August/006439.html for a detailed explanation. – fc7 Jun 20 '17 at 06:31

1 Answers1

0

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.

Guillotine1789
  • 342
  • 4
  • 12