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:
JavaScript | Java |
---|---|
boolean primitive | boolean |
boolean primitive | java.lang.Boolean |
null | null |
number primitive | byte |
number primitive | char |
number primitive | double |
number primitive | float |
number primitive | int |
number primitive | java.lang.Double |
number primitive | long |
number primitive | short |
string primitive | java.lang.String |
Function object | netscape.javascript.JSObject |
JavaArray object | netscape.javascript.JSObject |
JavaClass object | netscape.javascript.JSObject |
JavaMethod object | netscape.javascript.JSObject |
JavaObject object | The encapsulated Java object unwrapped |
JavaPackage object | netscape.javascript.JSObject |
netscape.javascript.JSObject | java.lang.String |
All other JavaScript objects | netscape.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.
Be aware that you can lose precision when passing data into and out of a Java applet method. As well as type conversion, value truncation may occur.
JavaScript number values can interface to java.lang.Double method arguments but not to java.lang.Integer or java.lang.Float.
Because JavaScript does not have a character data type, numbers can map to Java char data types. A character in a string in JavaScript is presented as a String primitive but is only one character long. This will convert to java.lang.String but will not naturally convert to a Java char data value unless you make some effort in the script to prepare it first. A String.charCodeAt() conversion may be necessary.
Some Java encapsulations are not unwrapped when the value is passed to Java. A JavaPackage, JavaArray, JavaClass and the deprecated JavaMethod are all encapsulated as a JSObject. This may cause problems if the objects are passed backwards and forwards as the encapsulation might become increasingly nested. This behaviour would be classed as erroneous and may be platform dependent. It may also depend on what happens inside the applet code when the method is called.
JavaScript arrays are not converted to Java arrays of JSObject items. They are passed as a JSObject encapsulated as a whole.
Prev | Home | Next |
JavaScript Style Sheets | Up | JavaScript version |
JavaScript Programmer's Reference, Cliff Wootton Wrox Press (www.wrox.com) Join the Wrox JavaScript forum at p2p.wrox.com Please report problems to support@wrox.com © 2001 Wrox Press. All Rights Reserved. Terms and conditions. |