You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Nov 17, 2022. It is now read-only.
Copy file name to clipboardExpand all lines: docs/core-concepts/android-runtime/marshalling/java-to-js.md
+47-13Lines changed: 47 additions & 13 deletions
Original file line number
Diff line number
Diff line change
@@ -8,15 +8,15 @@ position: 2
8
8
# Java to JavaScript Conversion
9
9
The article lists the available types in Java and how they are projected to JavaScript.
10
10
11
-
###String & Character
11
+
## String & Character
12
12
Both [java.lang.String](http://developer.android.com/reference/java/lang/String.html) and [java.lang.Character](http://docs.oracle.com/javase/7/docs/api/java/lang/Character.html) types are projected as JavaScript [String](http://www.w3schools.com/jsref/jsref_obj_string.asp):
13
13
14
14
```javascript
15
15
var file =newjava.io.File("/path/to/file");
16
16
var path =file.getPath(); // returns java.lang.String, converted to JS String
17
17
```
18
18
19
-
###Boolean & Primitive boolean
19
+
## Boolean & Primitive boolean
20
20
Both the primitive [boolean](http://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html) and reference [java.lang.Boolean](http://docs.oracle.com/javase/7/docs/api/java/lang/Boolean.html) types are projected as JavaScript [Boolean](http://www.w3schools.com/jsref/jsref_obj_boolean.asp):
21
21
22
22
```javascript
@@ -25,47 +25,47 @@ var button = new android.widget.Button(context);
25
25
var enabled =button.isEnabled(); // returns primitive boolean, converted to JS Boolean
26
26
```
27
27
28
-
###Byte & Primitive byte
28
+
## Byte & Primitive byte
29
29
Both the primitive [byte](http://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html) and reference [java.lang.Byte](http://docs.oracle.com/javase/7/docs/api/java/lang/Byte.html) types are projected as JavaScript [Number](http://www.w3schools.com/jsref/jsref_obj_number.asp):
30
30
31
31
```javascript
32
32
var byte =newjava.lang.Byte("1");
33
33
var jsByteValue =byte.byteValue(); // returns primitive byte, converted to Number
34
34
```
35
35
36
-
###Short & Primitive short
36
+
## Short & Primitive short
37
37
Both the primitive [short](http://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html) and reference [java.lang.Short](http://docs.oracle.com/javase/7/docs/api/java/lang/Short.html) types are projected as JavaScript [Number](http://www.w3schools.com/jsref/jsref_obj_number.asp):
38
38
39
39
```javascript
40
40
var short =newjava.lang.Short("1");
41
41
var jsShortValue =short.shortValue(); // returns primitive short, converted to Number
42
42
```
43
43
44
-
###Integer & Primitive int
44
+
## Integer & Primitive int
45
45
Both the primitive [int](http://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html) and reference [java.lang.Integer](http://docs.oracle.com/javase/7/docs/api/java/lang/Integer.html) types are projected as JavaScript [Number](http://www.w3schools.com/jsref/jsref_obj_number.asp):
46
46
47
47
```javascript
48
48
var int =newjava.lang.Integer("1");
49
49
var jsIntValue =int.intValue(); // returns primitive int, converted to Number
50
50
```
51
51
52
-
###Float & Primitive float
52
+
## Float & Primitive float
53
53
Both the primitive [float](http://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html) and reference [java.lang.Float](http://docs.oracle.com/javase/7/docs/api/java/lang/Float.html) types are projected as JavaScript [Number](http://www.w3schools.com/jsref/jsref_obj_number.asp):
54
54
55
55
```javascript
56
56
var float =newjava.lang.Float("1.5");
57
57
var jsFloatValue =float.floatValue(); // returns primitive float, converted to Number
58
58
```
59
59
60
-
###Double & Primitive double
60
+
## Double & Primitive double
61
61
Both the primitive [double](http://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html) and reference [java.lang.Double](http://docs.oracle.com/javase/7/docs/api/java/lang/Double.html) types are projected as JavaScript [Number](http://www.w3schools.com/jsref/jsref_obj_number.asp):
62
62
63
63
```javascript
64
64
var double =newjava.lang.Double("1.5");
65
65
var jsDoubleValue =double.doubleValue(); // returns primitive double, converted to Number
66
66
```
67
67
68
-
###Long & Primitive long
68
+
## Long & Primitive long
69
69
[java.lang.Long](http://docs.oracle.com/javase/7/docs/api/java/lang/Long.html) and its primitive equivalent are special types which are projected to JavaScript by applying the following rules:
70
70
71
71
* If the value is in the interval (-2^53, 2^53) then it is converted to [Number](http://www.w3schools.com/jsref/jsref_obj_number.asp)
@@ -92,7 +92,7 @@ var jsNumber = testClass.getLongNumber53Bits(); // result is JavaScript Number
92
92
var specialObject =testClass.getLongNumber54Bits(); // result is the special object described above
93
93
```
94
94
95
-
###Array
95
+
## Array
96
96
Array in Java is a special [java.lang.Object](http://docs.oracle.com/javase/7/docs/api/java/lang/Object.html) that have an implicit Class associated. A Java Array is projected to JavaScript as a special JavaScript proxy object with the following characteristics:
97
97
98
98
* Has length property
@@ -108,6 +108,7 @@ var singleFile = files[0]; // the indexed getter callback is triggered and a pro
108
108
109
109
>**Note:** A Java Array is intentionally not converted to a JavaScript [Array](http://www.w3schools.com/jsref/jsref_obj_array.asp) for the sake of performance, especially when it comes to large arrays.
110
110
111
+
### Array of Objects
111
112
112
113
Occasionally you have to create Java arrays from JavaScript. For this scenario we added method `create` to built-in JavaScript [`Array` object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array). Here are some examples how to use `Array.create` method:
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
130
+
```JavaScript
130
131
// equivalent to int[][] jaggedIntArray2 = new int[10][];
131
132
var jaggedIntArray2 =Array.create("[I", 10);
132
133
@@ -137,15 +138,48 @@ var jaggedBooleanArray3 = Array.create("[[Z", 10);
137
138
var jaggedObjectArray4 =Array.create("[[[Ljava.lang.Object;", 10);
138
139
```
139
140
The second signature uses `javaClassCtorFunction` which must the JavaScript constructor function for a given Java type. Here are some examples:
140
-
```javascript
141
+
```JavaScript
141
142
// equivalent to String[] stringArr = new String[10];
142
143
var stringArr =Array.create(java.lang.String, 10);
143
144
144
145
// equivalent to Object[] objectArr = new Object[10];
145
146
var objectArr =Array.create(java.lang.Object, 10);
146
147
```
147
148
148
-
### Null
149
+
### Array of Primitive Types
150
+
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, you need to convert it as follows:
151
+
```Java
152
+
publicstaticvoid myMethod(int[] someParam)
153
+
```
154
+
Then yoy need to invoke it as follows:
155
+
```JavaScript
156
+
let arr =Array.create("int", 3);
157
+
arr[0] =1;
158
+
arr[1] =2;
159
+
arr[2] =3;
160
+
161
+
SomeObject.myMethod(arr); // assuming the method is accepting an array of primitive types
162
+
```
163
+
164
+
### Two-Dimensional Arrays of Primitive Types
165
+
166
+
The above scenario gets more tricky with two-dimensional arrays. Consider a Java method that accepts as an argument a two-dimensional array:
The marshalled JavaScript code will look like this:
171
+
```JavaScript
172
+
let arr =Array.create("[Ljava.lang.Integer;", 2);
173
+
let elements =Array.create("java.lang.Integer", 3);
174
+
elements[0] =newjava.lang.Integer(1);
175
+
elements[1] =newjava.lang.Integer(2);
176
+
elements[2] =newjava.lang.Integer(3);
177
+
arr[0] = elements;
178
+
179
+
SomeObject.myMethod(arr); // assuming the method is accepting a two-dimensional array of primitive types
180
+
```
181
+
182
+
## Null
149
183
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):
150
184
151
185
```javascript
@@ -154,7 +188,7 @@ var button = new android.widget.Button(context);
154
188
var background =button.getBackground(); // if there is no background drawable method will return JS null
155
189
```
156
190
157
-
###Android Types
191
+
## Android Types
158
192
All Android-declared types are projected to JavaScript using the Package and Class proxies as described in [Accessing APIs](../metadata/accessing-packages.md)
0 commit comments