Java Reference
In-Depth Information
/* Call the Array.prototype.join() function on a
String object */
// Get the reference of the Array.prototype.join method
ScriptObjectMirror arrayObject =
(ScriptObjectMirror)engine.eval(
"Array.prototype");
ScriptObjectMirror joinFunc =
(ScriptObjectMirror)arrayObject.
getMember("join");
// Call the join() function of Array.prototype on a
// string object passing a hyphen as a separator
String thisObject = "Hello";
String separator = "-";
String joinResult = (String)joinFunc.
call(thisObject, separator);
System.out.println(joinResult);
}
catch (Exception e) {
e.printStackTrace();
}
}
}
Factorial of 5 is 120.0
H-e-l-l-o
The
ScriptObjectMirror
class contains several other methods to deal with script
objects in Java code. You can use the
seal()
,
freeze()
, and
preventExtensions()
methods to seal, freeze, and prevent extensions of the script object; they work the same
as the
Object.seal()
,
Object.freeze()
, and
Object.preventExtensions()
methods
in a Nashorn script. The class contains a utility method called
to()
, which is declared as
follows:
<T> T to(Class<T> type)
You can use the
to()
method to convert a script object to the specified
type
.
Search WWH ::
Custom Search