Previous     Contents     Index     Next     
Core JavaScript Reference 1.5





JavaArray

A wrapped Java array accessed from within JavaScript code is a member of the type JavaArray.


Core object  

 

Implemented in  

JavaScript 1.1, NES 2.0  


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 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.


Property Summary      


Property

Description

length

 

The number of elements in the Java array represented by JavaArray.  


Method Summary


Method

Description

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 1. 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 2. 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)

length

The number of elements in the Java array represented by the JavaArray object.



Property of  

JavaArray  

Implemented in  

JavaScript 1.1, NES 2.0  


Description
Unlike Array.length, JavaArray.length is a read-only property. You cannot change the value of the JavaArray.length property because Java arrays have a fixed number of elements.


See also
Array.length


toString

Returns a string representation of the JavaArray.



Method of  

JavaArray  

Implemented in  

JavaScript 1.1, NES 2.0  


Parameters
None


Description
Calls the method java.lang.Object.toString, which returns the value of the following expression:

JavaArray.getClass().getName() + '@' +
     java.lang.Integer.toHexString(JavaArray.hashCode())


Backward compatibility

JavaScript 1.3 and earlier. The toString method is inherited from the Object object and returns the following value:

[object JavaArray]


Previous     Contents     Index     Next     
Copyright © 2000 Netscape Communications Corp. All rights reserved.

Last Updated September 28, 2000