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
+12-11Lines changed: 12 additions & 11 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:
113
114
@@ -178,7 +179,7 @@ arr[0] = elements;
178
179
SomeObject.myMethod(arr); // assuming the method is accepting a two-dimensional array of primitive types
179
180
```
180
181
181
-
###Null
182
+
## Null
182
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):
183
184
184
185
```javascript
@@ -187,7 +188,7 @@ var button = new android.widget.Button(context);
187
188
var background =button.getBackground(); // if there is no background drawable method will return JS null
188
189
```
189
190
190
-
###Android Types
191
+
## Android Types
191
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