gpt4 book ai didi

Java 到 javascript 类型转换

转载 作者:行者123 更新时间:2023-11-30 18:47:56 24 4
gpt4 key购买 nike

谁能解释一下这是什么意思?

Boxing objects for Java numeric values(the java.lang classes Byte,Character, Short, Int, Long, Float,and Double) are unboxed and convertedto the closest available JavaScriptnumeric type, except when they are thedeclared return type from a method orthe result of a new expression usingthe per-applet Packages keyword. Inthis case, the boxing object will bereturned to the JavaScript engine as aJava object.

A Java Boolean isconverted to a JavaScript boolean,except when it is the declared returntype from a method or the result of anew expression using the per-appletPackages keyword. In this case, theboxing object will be returned to theJavaScript engine as a Java object.

Java Strings are converted toJavaScript strings, except when theyare the result of a new expressionusing the per-applet Packages keyword.In this case, the Java String isreturned to the JavaScript engine as aJava object.

这是来自 liveconnect specification但我不明白什么时候java类型会被转换成javascript类型,什么时候不会。 except 部分是什么意思?有例子吗?

最佳答案

假设你有

Integer i;
i = 1;

然后 int 1 自动转换为 Integer 对象;这就是自动装箱。

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,

意味着以这种方式转换的整数将恢复为原生 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.

...但是如果您声明了一个返回整数的函数,它将被转换为 Java 对象表示。这也适用于您使用此 Packages 关键字创建的内容。 (我不知道这是什么,但就是这个意思。)

更新:这是规范中的示例:

在 JS 代码中:

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

意思是说,如果 val 是 Java 中的一个 Integer,在这种情况下它就变成了 javascript 中的一个 JSObject

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.

类似地,如果你有一个 Java 字符串

String s = "I am a string."

使用 Java 内部表示和方法,它将被转换为 javscrip0t 字符串(不同的实现和方法),除非您使用 Packages 关键字创建它。

关于Java 到 javascript 类型转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4847946/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com