Table of Contents | Previous | Next | Index


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.

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.

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 Client-Side JavaScript Guide for more information about data type conversions.

Property Summary      

Property Description
length

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

Method Summary

Method Description
toString

Returns a string identifying the object as a JavaArray.

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.

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

The toString method is inherited from the Object object and returns the following value:

[object JavaArray]

Table of Contents | Previous | Next | Index

Last Updated: 05/28/99 11:59:40

Copyright (c) 1999 Netscape Communications Corporation

Any sample code included above is provided for your use on an "AS IS" basis, under the Netscape License Agreement - Terms of Use