Java Reference
In-Depth Information
public static void printMap(Map<?,?> phoneMap) {
System.out.println("Inside printMap():");
phoneMap.forEach((key, value) -> {
System.out.println("key = " + key + ", value = " +
value);
});
}
}
Consider the Nashorn script as shown in Listing 7-8. It passes Nashorn arrays to Java
methods that expects arrays, List s, and Map s. The output proves that all methods are
called and Nashorn runtime provides automatic conversions. Notice that when a Nashorn
array contains elements of types not matching Java types, those elements are converted to
appropriate Java types according to Nashorn type conversion rules. For example, a string
in Nashorn is converted to 0 as Java int .
Listing 7-8. A Nashorn Script to test the ArrayConversion Class
// arrayconversion.js
var ArrayConversion = Java.type("com.jdojo.script.ArrayConversion");
ArrayConversion.printInts([1, 2, 3]);
ArrayConversion.printInts([1, "Hello", 3]); // "hello" is converted to 0
// Non-integers will be converted to corresponding integers per Nashorn rules
// when a Nashorn array is converted to a Java array. true and false are
// converted to 1 and 0, and 10.3 is converted to 10.
ArrayConversion.printInts([1, true, false, 10.3]);
// Nashorn array to Java double[] conversion
ArrayConversion.printDoubles([10.89, "Hello", 3]);
// Nashorn array to Java List conversion
ArrayConversion.printList([10.89, "Hello", 3]);
// Nashorn array to Java Map conversion
ArrayConversion.printMap([10.89, "Hello", 3]);
Search WWH ::




Custom Search