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 to do it...

  1. Let's create a List of numbers using the Arrays.asList API:
        var numbers = java.util.Arrays.asList(12,4,5,67,34,567,32);
  1. Now, compute the maximum number in the list:
        var max = java.util.Collections.max(numbers);
  1. We can print max using the JavaScript print() method, and we can use template strings:
        print(`Max of ${numbers} is ${max}`);
  1. Let's run the script created using jjs:
        jjs --language=es6 java_from_javascript.js
  1. Now, let's import the java.util package:
        var javaUtils = new JavaImporter(java.util);
  1. Let's use the imported package to print today's date:
        with(javaUtils){          var date = new Date();          print(`Todays date is ${date}`);        }
  1. Let's create an alias for a Java type using the Java.type API:
        var jSet = Java ...

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