Summary
Core Object
A wrapped Java array accessed from within JavaScript code is a member of the type JavaArray
.
Created by
Any Java method which returns an array. In addition, you can create a JavaArray
with an arbitrary data type using the newInstance
method of the Array
class:
public static Object newInstance(Class componentType, int length) throws NegativeArraySizeException
Description
The JavaArray
object is an instance of a Java array that is created in or passed to JavaScript. JavaArray
is a wrapper for the instance; all references to the array instance are made through the JavaArray
.
In JavaScript 1.4 and later, the componentType
parameter is either a JavaClass
object representing the type of the array or class object, such as one returned by java.lang.Class.forName
. In JavaScript 1.3 and earlier, componentType
must be a class object.
Use zero-based indexes to access the elements in a JavaArray
object, just as you do to access elements in an array in Java. For example:
var javaString = new java.lang.String("Hello world!"); var byteArray = javaString.getBytes(); byteArray[0] // returns 72 byteArray[1] // returns 101
Any Java data brought into JavaScript is converted to JavaScript data types. When the JavaArray
is passed back to Java, the array is unwrapped and can be used by Java code. See the Core JavaScript 1.5 Guide for more information about data type conversions.
In JavaScript 1.4 and later, the methods of java.lang.Object
are inherited by JavaArray
.
Backward compatibility
JavaScript 1.3 and earlier
The methods of java.lang.Object
are not inherited by JavaArray
. In addition, the toString
method is inherited from the Object
object and returns the following value:
[object JavaArray]
You must specify a class object, such as one returned by java.lang.Object.forName
, for the componentType
parameter of newInstance
when you use this method to create an array. You cannot use a JavaClass
object for the componentType
parameter.
Properties
length: The number of elements in the Java array represented by JavaArray
.
Methods
toString: In JavaScript 1.4, this method is overridden by the inherited method java.lang.Object.toString
. in JavaScript 1.3 and earlier, this method returns a string identifying the object as a JavaArray
.
In JavaScript 1.4 and later, JavaArray
also inherits methods from the Java array superclass, java.lang.Object
.
Examples
Example: Instantiating a JavaArray
in JavaScript
In this example, the JavaArray
byteArray
is created by the java.lang.String.getBytes
method, which returns an array.
var javaString = new java.lang.String("Hello world!"); var byteArray = javaString.getBytes();
Example: Instantiating a JavaArray
in JavaScript with the newInstance
method
In JavaScript 1.4, you can use a JavaClass
object as the argument for the newInstance
method which creates the array, as shown in the following code:
var dogs = java.lang.reflect.Array.newInstance(java.lang.String, 5);
In JavaScript 1.1, use a class object returned by java.lang.Class.forName
as the argument for the newInstance method, as shown in the following code:
var dataType = java.lang.Class.forName("java.lang.String"); var dogs = java.lang.reflect.Array.newInstance(dataType, 5);