Skip to content
This repository was archived by the owner on Nov 17, 2022. It is now read-only.

Commit 6fea666

Browse files
committed
feat: add marshalling example for Java array of primitive types
1 parent 341337f commit 6fea666

File tree

1 file changed

+35
-2
lines changed

1 file changed

+35
-2
lines changed

docs/core-concepts/android-runtime/marshalling/java-to-js.md

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ Array.create(elementClassName, length)
126126
Array.create(javaClassCtorFunction, length)
127127
```
128128
The first signature accepts `string` for `elementClassName`. This option is useful when you have to create Java array of primitive types (e.g. `char`, `boolean`, `byte`, `short`, `int`, `long`, `float` and `double`). It is also useful when you have to create Java jagged arrays. For this scenario `elementClassName` must be the standard JNI class notation. Here are some examples:
129-
```javascript
129+
```JavaScript
130130
// equivalent to int[][] jaggedIntArray2 = new int[10][];
131131
var jaggedIntArray2 = Array.create("[I", 10);
132132

@@ -137,14 +137,47 @@ var jaggedBooleanArray3 = Array.create("[[Z", 10);
137137
var jaggedObjectArray4 = Array.create("[[[Ljava.lang.Object;", 10);
138138
```
139139
The second signature uses `javaClassCtorFunction` which must the JavaScript constructor function for a given Java type. Here are some examples:
140-
```javascript
140+
```JavaScript
141141
// equivalent to String[] stringArr = new String[10];
142142
var stringArr = Array.create(java.lang.String, 10);
143143

144144
// equivalent to Object[] objectArr = new Object[10];
145145
var objectArr = Array.create(java.lang.Object, 10);
146146
```
147147

148+
### Array of Primitive Types
149+
The automatic marshalling works only for cases with arrays of objects. In cases where you have a method that takes an array of primitive types, we need to convert as follows:
150+
```Java
151+
public static void myMethod(int[] someParam)
152+
```
153+
Then we need to invoke it as follows:
154+
```JavaScript
155+
let arr = Array.create("int", 3);
156+
arr[0] = 1;
157+
arr[1] = 2;
158+
arr[2] = 3;
159+
160+
SomeObject.myMethod(arr); // assuming the method is accepting an array of primitive types
161+
```
162+
163+
### Two-Dimensional Arrays of Primitive Types
164+
165+
The above scenario gets more tricky with two-dimensional arrays. Consider a Java method that accepts as an argument a two-dimensional array:
166+
```Java
167+
public static void myMethod(java.lang.Integer[][] someParam)
168+
```
169+
The JavaScritp marshaled code will look like this:
170+
```JavaScript
171+
let arr = Array.create("[Ljava.lang.Integer;", 2);
172+
let elements = Array.create("java.lang.Integer", 3);
173+
elements[0] = new java.lang.Integer(1);
174+
elements[1] = new java.lang.Integer(2);
175+
elements[2] = new java.lang.Integer(3);
176+
arr[0] = elements;
177+
178+
SomeObject.myMethod(arr); // assuming the method is accepting a two-dimensional array of primitive types
179+
```
180+
148181
### Null
149182
The Java [null literal](http://docs.oracle.com/javase/specs/jls/se7/html/jls-3.html#jls-3.10.7) (or null pointer) is projected to JavaScript [Null](http://www.w3schools.com/js/js_typeof.asp):
150183

0 commit comments

Comments
 (0)