开发者

Java to javascript type conversion

开发者 https://www.devze.com 2023-02-07 20:15 出处:网络
Can somebody explain what does this mean? Boxing objects for Java numeric values (the java.lang classes Byte,

Can somebody explain what does this mean?

Boxing objects for Java numeric values (the java.lang classes Byte, Character, Short, Int, Long, Float, and Double) are unboxed and converted to the closest available JavaScript numeric type, except when they are the declared return type from a method or the result of a new expression using the per-applet Packages keyword. In this case, the boxing object will be returned to the JavaScript engine as a Java object.

A Java Boolean is converted to a JavaScript boolean, except when it is the declared return type from a method or the result of a new e开发者_Python百科xpression using the per-applet Packages keyword. In this case, the boxing object will be returned to the JavaScript engine as a Java object.

Java Strings are converted to JavaScript strings, except when they are the result of a new expression using the per-applet Packages keyword. In this case, the Java String is returned to the JavaScript engine as a Java object.

This is from liveconnect specification but I can not understand when java type will be converted to javascript type and when it wouldn't. What does the except part mean? Any examples?


Let's say you have

Integer i;
i = 1;

Then the int 1 is converted to an Integer object automagically; that's autoboxing.

Boxing objects for Java numeric values (the java.lang classes Byte, Character, Short, Int, Long, Float, and Double) are unboxed and converted to the closest available JavaScript numeric type,

means that an integer converted that way will go back to being a native javascript int.

except when they are the declared return type from a method or the result of a new expression using the per-applet Packages keyword. In this case, the boxing object will be returned to the JavaScript engine as a Java object.

... but if you have declared a function to return an Integer, it will instead be converted to a Java object representation. This is also true of something you've created using this Packages keyword. (I don't know what this is, but that's what it means.)

Update: Here's an example from the spec:

in JS code:

val = new app.Packages.com.mycompany.MyClass();

It's saying that if val is, say, an Integer in Java, in this case it becomes a JSObject in javascript.

Java Strings are converted to JavaScript strings, except when they are the result of a new expression using the per-applet Packages keyword. In this case, the Java String is returned to the JavaScript engine as a Java object.

Similarly, if you have a Java string

String s = "I am a string."

with the Java internal representation and methods, it will be converted intoa javscrip0t string (different implementation and methods) unless you created it with the Packages keyword.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号