Java Reference
In-Depth Information
In a scripting language, the type of a variable is known at runtime, not at compile
time. The interpreters of scripting languages resolve an overloaded method call
appropriately depending on the runtime type of the arguments in a method call. The
output of the following JavaScript code shows that the call to the print() method of the
PrintTest class is resolved at runtime depending on the type of the argument. You may
get a bit different output in the second line:
// JavaScript Code
// Create an object of the Java class called PrintTest
var PrintTest = Java.type("com.jdojo.script.PrintTest");
var pt = new PrintTest();
// Create a JavaScript array with three elements
var list = ["Hello", new Object(), 10.5];
// Call the overloaded method print() of the PrintTest class
// passing each object in an array at time
for each(var element in list) {
pt.print(element);
}
print(String): Hello
print(Object): jdk.nashorn.internal.scripts.JO@405818
print(Double): 10.5
JavaScript lets you select a specific version of the overloaded method explicitly. You
can pass the signature of the overloaded method to be invoked with the object reference.
The following snippet of code selects the print(Object) version:
// JavaScript Code
var PrintTest = Java.type("com.jdojo.script.PrintTest");
var pt = new PrintTest();
pt["print(java.lang.Object)"](10.5); // Calls print(Object)
pt["print(java.lang.Double)"](10.5); // Calls print(Double)
print(Object): 10.5
print(Double): 10.5
 
Search WWH ::




Custom Search