Reputation: 8487
Use java8 jjs to study jdk api and found this, see below:
jjs> var result = java.lang.Character.digit('473'.charAt(0),10)
jjs> result
-1
jjs> '473'.charAt(0)
4
but if I executed above code in java main, got 4.
System.out.println(Character.digit('4', 10));//4
why in jjs it returns -1?
Upvotes: 2
Views: 53
Reputation: 226
This has to do with the automatic resolution of Java methods. Since a char is internally represented as an int, Nashorn will resolve the digit method as the one accepting a Unicode codePoint (int) and radix.
If you want to make sure to call the digit method that accepts a character (char) and radix, you should explicitly resolve the method you want to invoke, like this:
$ jjs
jjs> var digit = java.lang.Character['digit(char,int)']
jjs> digit('473'.charAt(0), 10)
4
Upvotes: 3