JDK-8186140 : java long comparison with == operator returns incorrect result.
  • Type: Bug
  • Component: core-libs
  • Sub-Component: jdk.nashorn
  • Affected Version: 8u92
  • Priority: P3
  • Status: Resolved
  • Resolution: Not an Issue
  • OS: generic
  • CPU: generic
  • Submitted: 2017-08-07
  • Updated: 2018-01-30
  • Resolved: 2017-10-11
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 10
10Resolved
Related Reports
Relates :  
Description
FULL PRODUCT VERSION :
java version "1.8.0_144"
Java(TM) SE Runtime Environment (build 1.8.0_144-b01)
Java HotSpot(TM) 64-Bit Server VM (build 25.144-b01, mixed mode)

ADDITIONAL OS VERSION INFORMATION :
Darwin xx 16.7.0 Darwin Kernel Version 16.7.0: Thu Jun 15 17:36:27 PDT 2017; root:xnu-3789.70.16~2/RELEASE_X86_64 x86_64

A DESCRIPTION OF THE PROBLEM :
jrunscript

var a = new java.io.File("/bin/cat");

print(typeof a.length()); // object
print(a.length() == a.length()); // false

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
jrunscript

var a = new java.io.File("/bin/cat");

print(typeof a.length()); // object
print(a.length() == a.length()); // false

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
number
true
ACTUAL -
object
false

REPRODUCIBILITY :
This bug can be reproduced always.


Comments
In JDK9, optimistic-types is enabled by default. so the return type of length() is optimistically is set as int.Since the returning value a.length() can be represented as int, comparison of two int values returns true. With JDK8, optimistic-types is disabled by default, return type of a.length() is Object(long is removed as internal numeric type). comparison of two Objects with == returns false. User has to use Number(n) or +n(unary operator) when using == operator. (n -> long value) Otherwise in JDK 8, if optimistic-types is enabled, it will return true: NashornScriptEngine ENGINE = (NashornScriptEngine) new NashornScriptEngineFactory().getScriptEngine(new String[]{"--optimistic-types=true"});
11-10-2017

The type is an object and not a number because this was changed as part of JDK-8144020. However, the comparison on length is an issue. The test passes on JDK 9 in all the three OS. Following are the results : JDK 8u91-Pass JDK8u92 - Fail JDK 8u144 - Fail JDK 9-ea + 181 -Pass Following is the output on running the test case on JDK 8u144 on Mac-osx: apple:pallavi jpse$ /macosx-x64/bin/jrunscript nashorn> var a = new java.io.File("/bin/cat"); nashorn> print(typeof a.length()); object nashorn> print(a.length() == a.length()); false nashorn> quit () Following is the output on JDK 9-ea + 181: apple:pallavi jpse$ /jdk/9/latest/binaries/osx-x64/bin/jrunscript nashorn> var a = new java.io.File("/bin/cat"); nashorn> print(typeof a.length()); object nashorn> print(a.length() == a.length()); true nashorn> quit()
11-08-2017