When a Java method returns a value to JavaScript, the Java values need to be converted to compatible and useful data types. This is accomplished with LiveConnect in Netscape Navigator and ActiveX in MSIE.
In some cases a single Java primitive data type will be compatible with JavaScript primitives and objects of many different types. An example of this is the JavaScript Number primitive that will readily accept half a dozen different Java numeric and logical types.
Some Java data types correspond exactly to a single and specific JavaScript type. Examples of this are null, Boolean and void.
A third possibility is that many different Java object types will become a single JavaScript object type. In fact Java objects all become a generic JavaObject.
Here is a table that summarises the correspondence between Java and JavaScript types when converting to Java. This would be used when passing values into methods under control of JavaScript:
Java | JavaScript |
---|---|
boolean | boolean primitive |
byte | number primitive |
char | number primitive |
double | number primitive |
float | number primitive |
int | number primitive |
long | number primitive |
null | null |
short | number primitive |
void | undefined |
java.lang.Boolean | JavaObject object |
java.lang.Character | JavaObject object |
java.lang.Class | JavaObject object |
java.lang.Double | JavaObject object |
java.lang.Float | JavaObject object |
java.lang.Integer | JavaObject object |
java.lang.Long | JavaObject object |
java.lang.String | JavaObject object |
All Java arrays regardless of what they contain | JavaArray |
netscape.javascript.JSObject | generic JavaScript object |
All other Java objects | JavaObject object |
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 method return interface. JavaScript values may need to be converted further if they are assigned to JavaScript LValues.
JavaScript objects are wrapped so that Java can access them as a netscape.javascript.JSObject object. When they are passed back to JavaScript, the encapsulation is removed and the intrinsic JavaScript object is accessed directly again.
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. You may not get out what you put in - in terms of accuracy. An example of this is passing a number from JavaScript to a short primitive data type inside Java and back again.
JavaScript number values can interface to java.lang.Double method arguments but will be mapped back to a JavaObject. You may need to extract the numeric value to yield a number primitive again.
Because JavaScript does not have a character data type, the Java char values will be converted to number primitives. You will need to convert them to strings yourself by converting the character's numeric value to a character with the String.fromCharCode() method.
Although number primitives in JavaScript can convert to a java.lang.Double value, converting back again yields a JavaObject and not a number primitive. You will need to extract the numeric value again manually.
The java.lang.String object becomes a JavaObject. This means the String and netscape.javascript.JSObject items mutate as they pass into Java and back again. You may need to do additional work to get the string value back out in a form that you need.
All Java arrays are returned in a JavaArray object. This means that arrays of strings, numbers and objects all appear to be the same from JavaScript and you may need to provide special code to unwrap them properly.
Prev | Home | Next |
Java method data conversion | Up | java |
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. |