O'Reilly logo

Beginning Java 8 APIs, Extensions and Libraries Swing, JavaFX, JavaScript, JDBC and Network Programming APIs by Kishori Sharan

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

// Create an int array of 2 elements, populate it, and print the elementsvar intArray = java.lang.reflect.Array.newInstance(java.lang.Integer.TYPE, 2);intArray[0] = 100;intArray[1] = 200;for(var i = 0; i < intArray.length; i++) {        print(intArray[i]);}
100200

Nashorn supports a new syntax to create Java arrays. First, create the appropriate Java array type using the Java.type() method, and then use the familiar new operator to create the array. The following snippet of code shows how to create a String[] of two elements in Nashorn:

// Get the java.lang.String[] typevar StringArray = Java.type("java.lang.String[]");// Create a String[] array of 2 elementsvar strArray = new StringArray (2);strArray[0] = "Hello";strArray[1] = "Array";for(var ...

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