Is this a bug? Inconsistent Java type for modulo operator

David P. Caldwell david at code.davidpcaldwell.com
Tue Sep 23 13:17:16 UTC 2014


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