Java 到 javascript 类型转换

发布于 2024-10-14 19:22:13 字数 741 浏览 9 评论 0原文

有人能解释一下这是什么意思吗?

对 Java 数值对象进行装箱 (java.lang 类 Byte, 字符、短整型、整数、长整型、浮点型、 和 Double)已拆箱并转换 最接近的可用 JavaScript 数字类型,除非当它们是 从方法声明的返回类型或 使用新表达式的结果 每个小程序的 Packages 关键字。在 在这种情况下,装箱对象将是 作为一个返回给 JavaScript 引擎 Java 对象。

Java 布尔值是 转换为 JavaScript 布尔值, 除非是申报回报 来自方法或结果的类型 使用每个小程序的新表达式 包关键字。在这种情况下, 装箱对象将被返回到 作为 Java 对象的 JavaScript 引擎。

Java 字符串转换为 JavaScript 字符串,除非 是新表达式的结果 使用 per-applet Packages 关键字。 在本例中,Java 字符串是 作为一个返回给 JavaScript 引擎 Java 对象。

这是来自 liveconnect 规范 但我不明白 java 类型何时会转换为javascript 类型以及什么时候不会。 除了部分是什么意思?有什么例子吗?

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 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 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

极致的悲 2024-10-21 19:22:13

假设您有

Integer i;
i = 1;

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

对 Java 数值对象进行装箱
(java.lang 类 Byte,
字符、短整型、整数、长整型、浮点型、
和 Double)已拆箱并转换
最接近的可用 JavaScript
数字类型,

意味着以这种方式转换的整数将恢复为原生 javascript int

除非它们已声明
方法或方法的返回类型
使用新表达式的结果
每个小程序的 Packages 关键字。在这个
在这种情况下,拳击对象将是
作为一个返回给 JavaScript 引擎
Java 对象。

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

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

在 JS 代码中:

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

它说如果 val 是,比如说,Java 中的 Integer,在这种情况下,它成为 javascript 中的 JSObject

Java 字符串转换为
JavaScript 字符串,除非它们
是新表达式的结果
使用 per-applet Packages 关键字。
在本例中,Java 字符串是
作为一个返回给 JavaScript 引擎
Java 对象。

同样,如果您有一个具有

String s = "I am a string."

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文