O'Reilly logo

Java 9 Cookbook by Nick Samoylov, Mohamed Sanaulla

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

How it works...

The Java types and their APIs can be accessed from the JavaScript code by using their fully qualified name, as we saw in the previous section while creating the list of numbers using java.util.Arrays.asList() and finding the maximum using java.util.Collections.max().  

If we want to skip specifying the package name along with the class name, we can make use of JavaImporter to import the packages and use the with clause to wrap the code, which uses the classes from the imported package within it, as follows:

var javaUtils = new JavaImporter(java.util);with(javaUtils){  var date = new Date();  print(`Todays date is ${date}`);}

The other feature we saw was creating a type alias for the Java type by using Java.type(<fully qualified ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required