Automatic type conversions: Passing char from Java to JS

Jim Laskey (Oracle) james.laskey at oracle.com
Thu Jul 10 00:14:43 UTC 2014


It is.  There is no char type in JS, so it is treated like other POJOs.  You have to explicitly char.toString();  or String(char).  Auto conversion would be simple to implement but you would lose the functionality of Character;

jjs>  var c = new java.lang.Character('z');
jjs> c.compareTo(c);                        
0


Cheers,

-- Jim





On Jul 9, 2014, at 1:15 PM, Tim Fox <timvolpe at gmail.com> wrote:

> 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