aboutsummaryrefslogtreecommitdiff
path: root/files/es/archive/web/liveconnect/javaarray/index.html
blob: 1ec785bfc5d78d8139869fc510afbe3d283c69c9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
---
title: JavaArray
slug: Archive/Web/LiveConnect/JavaArray
translation_of: Archive/Web/LiveConnect_Reference/JavaArray
---
<p>
</p>
<h3 id="Summary" name="Summary"> Summary </h3>
<p><b>Core Object</b>
</p><p>A wrapped Java array accessed from within JavaScript code is a member of the type <code>JavaArray</code>.
</p>
<h3 id="Created_by" name="Created_by"> Created by </h3>
<p>Any Java method which returns an array. In addition, you can create a <code>JavaArray</code> with an arbitrary data type using the <code>newInstance</code> method of the <code>Array</code> class:
</p>
<pre class="eval">public static Object newInstance(Class componentType,
   int length)
   throws NegativeArraySizeException
</pre>
<h3 id="Description" name="Description"> Description </h3>
<p>The <code>JavaArray</code> object is an instance of a Java array that is created in or passed to JavaScript. <code>JavaArray</code> is a wrapper for the instance; all references to the array instance are made through the <code>JavaArray</code>.
</p><p>In JavaScript 1.4 and later, the <code>componentType</code> parameter is either a <code>JavaClass</code> object representing the type of the array or class object, such as one returned by <code>java.lang.Class.forName</code>. In JavaScript 1.3 and earlier, <code>componentType</code> must be a class object.
</p><p>Use zero-based indexes to access the elements in a <code>JavaArray</code> object, just as you do to access elements in an array in Java. For example:
</p>
<pre class="eval">var javaString = new java.lang.String("Hello world!");
var byteArray = javaString.getBytes();
byteArray[0] // returns 72
byteArray[1] // returns 101
</pre>
<p>Any Java data brought into JavaScript is converted to JavaScript data types. When the <code>JavaArray</code> is passed back to Java, the array is unwrapped and can be used by Java code. See the <a href="es/Core_JavaScript_1.5_Guide">Core JavaScript 1.5 Guide</a> for more information about data type conversions.
</p><p>In JavaScript 1.4 and later, the methods of <code>java.lang.Object</code> are inherited by <code>JavaArray</code>.
</p>
<h3 id="Backward_compatibility" name="Backward_compatibility"> Backward compatibility </h3>
<h4 id="JavaScript_1.3_and_earlier" name="JavaScript_1.3_and_earlier"> JavaScript 1.3 and earlier </h4>
<p>The methods of <code>java.lang.Object</code> are not inherited by <code>JavaArray</code>. In addition, the <code>toString</code> method is inherited from the <code>Object</code> object and returns the following value:
</p>
<pre class="eval">[object JavaArray]
</pre>
<p>You must specify a class object, such as one returned by <code>java.lang.Object.forName</code>, for the <code>componentType</code> parameter of <code>newInstance</code> when you use this method to create an array. You cannot use a <code>JavaClass</code> object for the <code>componentType</code> parameter.
</p>
<h3 id="Properties" name="Properties"> Properties </h3>
<p><a href="es/Core_JavaScript_1.5_Reference/Global_Objects/JavaArray/length">length</a>: The number of elements in the Java array represented by <code>JavaArray</code>.
</p>
<h3 id="Methods" name="Methods"> Methods </h3>
<p><a href="es/Core_JavaScript_1.5_Reference/Global_Objects/JavaArray/toString">toString</a>: In JavaScript 1.4, this method is overridden by the inherited method <code>java.lang.Object.toString</code>. in JavaScript 1.3 and earlier, this method returns a string identifying the object as a <code>JavaArray</code>.
</p><p>In JavaScript 1.4 and later, <code>JavaArray</code> also inherits methods from the Java array superclass, <code>java.lang.Object</code>.
</p>
<h3 id="Examples" name="Examples"> Examples </h3>
<h4 id="Example:_Instantiating_a_JavaArray_in_JavaScript" name="Example:_Instantiating_a_JavaArray_in_JavaScript"> Example: Instantiating a <code>JavaArray</code> in JavaScript </h4>
<p>In this example, the <code>JavaArray</code> <code>byteArray</code> is created by the <code>java.lang.String.getBytes</code> method, which returns an array.
</p>
<pre class="eval">var javaString = new java.lang.String("Hello world!");
var byteArray = javaString.getBytes();
</pre>
<h4 id="Example:_Instantiating_a_JavaArray_in_JavaScript_with_the_newInstance_method" name="Example:_Instantiating_a_JavaArray_in_JavaScript_with_the_newInstance_method"> Example: Instantiating a <code>JavaArray</code> in JavaScript with the <code>newInstance</code> method </h4>
<p>In JavaScript 1.4, you can use a <code>JavaClass</code> object as the argument for the <code>newInstance</code> method which creates the array, as shown in the following code:
</p>
<pre class="eval">var dogs = java.lang.reflect.Array.newInstance(java.lang.String, 5);
</pre>
<p>In JavaScript 1.1, use a class object returned by <code>java.lang.Class.forName</code> as the argument for the newInstance method, as shown in the following code:
</p>
<pre class="eval">var dataType = java.lang.Class.forName("java.lang.String");
var dogs = java.lang.reflect.Array.newInstance(dataType, 5);
</pre>
<div class="noinclude">
</div>