Java Reference
In-Depth Information
jjs> var intArray = Java.type("int[]");
jjs> var intArr = new intArray(5);
jjs> intArr[0] = 0;
0
jjs> intArr[1] = 1;
1
jjs> intArr[0]
0
jjs> intArr.length
5
Working with collections is quite similar. To access a Java Collection type, you
call upon the Java.type function, passing the string-based name of the type you
want to create. Once the type reference has been obtained, it can be instantiated and ac-
cessed from JavaScript.
jjs> var ArrayList = Java.type("java.util.ArrayList")
jjs> var array = new ArrayList();
jjs> array.add('hi');
true
jjs> array.add('bye');
true
jjs> array
[hi, bye]
jjs> var map = Java.type("java.util.HashMap")
jjs> var jsMap = new map();
jjs> jsMap.put(0, "first");
null
jjs> jsMap.put(1, "second");
null
jjs> jsMap.get(1);
second
How It Works
To make use of Java arrays and collections from within JavaScript, you invoke the
Java.type() function and pass the name of the Java type that you want to access,
assigning it to a JavaScript variable. The JavaScript variable can then be instantiated
Search WWH ::




Custom Search