JavaScript to Java Conversion
The article lists the available types in JavaScript and how they are projected to Java.
String
JavaScript String maps to java.lang.String:
var context = ...;
var button = new android.widget.Button(context);
var text = "My Button"; // JavaScript string
button.setText(text); // text is converted to java.lang.String
Boolean
JavaScript Boolean maps to Java primitive boolean.
var context = ...;
var button = new android.widget.Button(context);
var enabled = false; // JavaScript Boolean
button.setEnabled(enabled); // enabled is converted to Java primitive boolean
Undefined & Null
JavaScript Undefined & Null maps to Java null literal (or null pointer).
var context = ...;
var button = new android.widget.Button(context);
button.setOnClickListener(undefined); // the Java call will be made using the null keyword
Number
Java has several primitive numeric types while JavaScript has the Number type only. Additionally, unlike JavaScript, Java is a language that supports Method Overloading, which makes the numeric conversion more complex. Consider the following Java class:
class MyObject extends java.lang.Object {
public void myMethod(byte value){
}
public void myMethod(short value){
}
public void myMethod(int value){
}
public void myMethod(long value){
}
public void myMethod(float value){
}
public void myMethod(double value){
}
}
The following logic applies when calling
myMethod
on a myObject
instance from
JavaScript:
var myObject = new MyObject();
- Implicit integer conversion:
myObject.myMethod(10); // myMethod(int) will be called.
Note: If there is no myMethod(int) implementation, the Runtime will try to choose the best possible overload with least conversion loss. If no such method is found an exception will be raised.
- Implicit floating-point conversion:
myObject.myMethod(10.5); // myMethod(double) will be called.
Note: If there is no myMethod(double) implementation, the Runtime will try to choose the best possible overload with least conversion loss. If no such method is found an exception will be raised.
-
Explicitly call an overload:
To enable developers call a specific method overload, the Runtime exposes the following functions directly in the global context:- byte(number) → Java primitive byte
The number value will be truncated and only its first byte of the whole part will be used.
- short(number) → Java primitive short
The number value will be truncated and only its first 2 bytes of the whole part will be used.
- float(number) → Java primitive float
The number value will be converted (with a possible precision loss) to a 2^32 floating-point value.
- long(number) → Java primitive long (in case the number literal fits JavaScript 2^53 limit)
The number value's whole part will be taken only.
- long("number") → Java primitive long (in case the number literal doesn't fit JavaScript 2^53 limit)
myObject.myMethod(byte(10)); // will call myMethod(byte)
myObject.myMethod(short(10)); // will call myMethod(short)
myObject.myMethod(float(10)); // will call myMethod(float)
myObject.myMethod(long(10)); // will call myMethod(long)
myObject.myMethod(long("123456")); // will convert "123456" to Java long and will call myMethod(long)
Note: When an explicit cast function is called and there is no such implementation found, the Runtime will directly fail, without trying to find a matching overload.
Array
A JavaScript Array is implicitly converted to a Java Array, using the above described rules for type conversion of the array's elements. For example:
class MyObject extends java.lang.Object {
public void myMethod(java.lang.String[] items){
}
}
var items = ["One", "Two", "Three"];
var myObject = new MyObject();
myObject.myMethod(items); // will convert to Java array of java.lang.String objects