Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
titlelambda as function example
var JFunction = Java.type('java.util.function.Function')

var obj = new JFunction() { 
   apply: function(x) { print(x*x) } 
}

// every lambda object is a 'function'

print(typeof obj); // prints "function"

// 'calls' lambda as though it is a function
obj(91);

Anchor
explicit_overload_resolution
explicit_overload_resolution

Explicit method signature

When you call java method from script, Nashorn automatically selects the most specific overload variant of that method (based on the runtime type of the actual arguments passed – with appropriate type conversions as needed). But sometimes you may want to manually select a specific overload variant. If so, you can specify method signature along with the method name.

 

Code Block
titleExplicit method signature
var out = java.lang.System.out;
out["println(int)"](Math.PI); // selects PrintStream.println(int).
// prints 3 as Math.PI is converted to int
Code Block
titleExplicit constructor selection (jdk9 only)
// With jdk9, you can select a specific constructor as well.

var C = java.awt["Color(int,int,int)"];
print(new C(255, 0, 0);

var F = Java.type("java.io.File")["(String)"];
print(new F("foo.txt"));

 

--no-java option

--no-java options can be used to switch-off Java specific extensions like "Java", "Packages" object etc.

...