JavaScript to Java values (Definition)

Conversion of JavaScript primitives and objects to Java compatible types.

When a Java method is called from JavaScript, the JavaScript values need to be converted to compatible and useful data types.

In some cases a single JavaScript type will be compatible with arguments of many different types. An example of this is the JavaScript Number primitive that will convert readily into half a dozen different Java types.

Some JavaScript data types correspond exactly to a single and specific Java type. An example of this is null.

A third possibility is that many different JavaScript types will become a single Java type. This is what happens to objects which all become a generic JavaScript object represented by the Java class JSObject.

Here is a table that summarises the correspondence between JavaScript and Java types when converting to Java. This would be used when passing values into methods under control of JavaScript:

JavaScriptJava
boolean primitiveboolean
boolean primitivejava.lang.Boolean
nullnull
number primitivebyte
number primitivechar
number primitivedouble
number primitivefloat
number primitiveint
number primitivejava.lang.Double
number primitivelong
number primitiveshort
string primitivejava.lang.String
Function objectnetscape.javascript.JSObject
JavaArray objectnetscape.javascript.JSObject
JavaClass objectnetscape.javascript.JSObject
JavaMethod objectnetscape.javascript.JSObject
JavaObject objectThe encapsulated Java object unwrapped
JavaPackage objectnetscape.javascript.JSObject
netscape.javascript.JSObjectjava.lang.String
All other JavaScript objectsnetscape.javascript.JSObject

When this happens under the control of Java, there are further limitations as to what can be exchanged between the environments. This is discussed under the individual methods that are affected in the JSObject description.

This table summarises the relationships at the passing interface. JavaScript values may have been converted during the expression evaluation. You may also need to coerce some values as they are passed so they are correctly mapped to the Java interface for the method.

Java objects are wrapped so that JavaScript can access them as a JavaObject object. When they are passed back to Java, the encapsulation is removed and the intrinsic Java object is accessed directly.

Warnings:

See also:Boolean, Function object, Java method data conversion, Java to JavaScript values, JSObject object, JSObject.call(), JSObject.eval(), JSObject.getMember(), JSObject.getSlot(), JSObject.toString(), LiveConnect, netscape.javascript.JSObject, null