Is this a bug? Inconsistent Java type for modulo operator
A. Sundararajan
sundararajan.athijegannathan at oracle.com
Tue Sep 23 17:06:02 UTC 2014
Hi,
The latest 8u-dev code @
http://hg.openjdk.java.net/jdk8u/jdk8u-dev/nashorn prints this:
eval = 0
eval2 = 1
eval.getClass() = class java.lang.Integer
eval2.getClass() = class java.lang.Integer
That said it is better to avoid depending on Java side value to be a
specific subtype of Number (like Integer, Double, Long). It is better to
expect and handle Numbar and call intValue(), doubleValue() on it. That
gives maximum freedom for Nashorn implementation. Nashorn tries to
represent int/long/double as appropriate for the specific cases.
-Sundar
On Tuesday 23 September 2014 06:47 PM, David P. Caldwell wrote:
> Asked by a StackOverflow user; I've produced a clearer reproduction case below.
>
> Original question is
> http://stackoverflow.com/questions/25989642/why-does-java-8-nashorn-javascript-modulo-returns-0-0-double-instead-of-0-i/25991982
>
> It confused the asker and I confess it makes no sense to me:
>
> public class Tester {
> public static void main( String[] args ) throws Exception {
> javax.script.ScriptEngine se = new
> javax.script.ScriptEngineManager().getEngineByName( "nashorn" );
>
> Object eval = se.eval( "5%5" );
> Object eval2 = se.eval( "5%2" );
>
> System.out.println( "eval = " + eval );
> System.out.println( "eval2 = " + eval2 );
> System.out.println( "eval.getClass() = " + eval.getClass() );
> System.out.println( "eval2.getClass() = " + eval2.getClass() );
> }
> }
>
> Result:
>
> $ java -version && javac Tester.java && java Tester
> java version "1.8.0_20"
> Java(TM) SE Runtime Environment (build 1.8.0_20-b26)
> Java HotSpot(TM) 64-Bit Server VM (build 25.20-b23, mixed mode)
> eval = 0.0
> eval2 = 1
> eval.getClass() = class java.lang.Double
> eval2.getClass() = class java.lang.Integer
More information about the nashorn-dev
mailing list