Automatic type conversions: Passing char from Java to JS

Tim Fox timvolpe at gmail.com
Wed Jul 9 16:15:38 UTC 2014


Hi,

I've noticed that if I pass a  java.lang.Character from Java to JS (e.g. 
via a JavaScript callback handler that is called from Java), then it is 
converted to a JavaScript object not a string.

I.e.

someJavaObject.setHandler(function(char) {
   console.log(typeof char);
});

Then in the Java object:

class MyJavaClass {

   public void setHandler(Consumer<Character> handler) {
      handler.accept('X');
   }

}

The following is logged:

object

If I instead pass a java.lang.String from Java to JS, it is converted to 
JS string as I would expect.

Intuitively I would expect a java.lang.Character to be converted to a JS 
string too.

Is the current Nashorn behaviour as expected?

Cheers




More information about the nashorn-dev mailing list