JDK是“向上”的吗?或“向后”。兼容的?

发布于 2024-10-12 11:21:59 字数 962 浏览 5 评论 0原文

向后二进制兼容性(或向下兼容性)- 使用旧版本库 API 构建的客户端在新版本库 API 上运行的能力 (wiki)。

向上二进制兼容性(或前向兼容性)- 使用新版本库 API 构建的客户端在旧版本库 API 上运行的能力 (wiki)。

Sun 关于 JDK 的一般文档 自 1.4.2 以来 J2SE 5.0 中的不兼容性 (和 Java SE 6 与 J2SE 5.0 的兼容性 也描述了兼容性JDK的如下:

JDK 5.0 与 Java 2 SDK v1.4.2 向上二进制兼容,但下面列出的不兼容性除外。这意味着,除了指出的不兼容性之外,使用版本 1.4.2 编译器构建的类文件将在 JDK 5.0 中正确运行

我认为文档编写者在这句话中混合了术语“向上”和“向后”兼容性。他们描述了“向后”兼容性,但将此功能称为“向上”兼容性。

这是拼写错误、错误还是本意? JDK 是“向上”还是“向后”兼容?

Backward binary compatibility (or downward compatibility) - an ability of clients built with an old version of library API to run on a new one (wiki).

Upward binary compatibility (or forward compatibility) - an ability of clients built with a new version of library API to run on old one (wiki).

The general Sun's document about JDK Incompatibilities in J2SE 5.0 since 1.4.2 (and Java SE 6 compatibility with J2SE 5.0 too) describes the compatibility of JDK as following:

JDK 5.0 is upwards binary-compatible with Java 2 SDK, v1.4.2 except for the incompatibilities listed below. This means that, except for the noted incompatibilities, class files built with version 1.4.2 compilers will run correctly in JDK 5.0.

I suppose that documentation writers have mixed up terms "upward" and "backward" compatibility in this sentence. They describe a "backward" compatibility, but call this feature as "upward" compatibility.

Is this a typo, mistake or intended term here?
Is JDK "upward" or "backward" compatible?

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

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

发布评论

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

评论(8

妄司 2024-10-19 11:21:59

请注意,要向后兼容的东西必须有一个向前兼容的对应物(有意或无意)。例如:DVD 阅读器是否向后兼容 CD,或者 CD 是否向前兼容 DVD 阅读器?

在这种情况下,这取决于您是否查看编译器(或其生成的字节码)或虚拟机。

该编译器不向后兼容,因为使用 Java5 JDK 生成的字节码无法在 Java 1.4 jvm 中运行(除非使用 -target 1.4 标志进行编译)。但 JVM 是向后兼容的,因为它可以运行较旧的字节码。

所以我猜他们选择从javac的角度考虑兼容性(因为它是JDK特有的部分),这意味着生成的字节码可以在jvm的未来版本中运行(与JRE更相关) ,而且还捆绑在 JDK 中)。

简而言之,我们可以说:

  • JDK(通常)是向前兼容的。
  • JRE(通常)是向后兼容的。

(这也是很久以前应该吸取的教训:编写编译器的人通常是对的,而我们使用它们的人是错误的 xD)

顺便说一句,将向后/向前和向下/向上而不是混合它们?

Note that for something to be backwards compatible there must be a counterpart that is forwards compatible (either intentionally or unintentionally). For example: are the DVD readers backwards compatible with CD's or are the CD's forward compatible with DVD readers?

In this case, it depends if you look at the compiler (or the bytecode it generates) or the virtual machine.

The compiler is not backwards compatible because bytecode generated with Java5 JDK won't run in Java 1.4 jvm (unless compiled with the -target 1.4 flag). But the JVM is backwards compatible, as it can run older bytecodes.

So I guess they chose to consider the compatibility from the point of view of javac (as it is the part specific to the JDK), meaning that the bytecode generated can be run in future releases of the jvm (that is more related to the JRE, but also bundled in the JDK).

In brief, we can say:

  • JDK's are (usually) forward compatible.
  • JRE's are (usually) backward compatible.

(And it also serves as a lesson that should be learnt long ago: the people writing the compilers are usually right, and we the people using them wrong xD)

By the way, doesn't it make more sense to pair backward/forward and downward/upward rather than mixing them up?

笑忘罢 2024-10-19 11:21:59

扩展答案以包括最新的 Java…

Java SE 7 和 JDK 7 兼容性

引自 Oracle 未注明日期的页面:

兼容性是一个复杂的问题。本文档讨论了三种类型
与 Java 版本相关的潜在不兼容性
平台:

  1. :源兼容性涉及将 Java 源代码转换为类文件,包括代码是否仍可在以下位置编译:
    全部。
  2. 二进制:Java 语言规范中将二进制兼容性定义为保留无错误链接的能力。
  3. 行为:行为兼容性包括运行时执行的代码的语义。

… 并且

Java SE 7 和 Java SE 6 之间的不兼容性 Java SE 7 与以前版本的 Java 平台高度兼容。
几乎所有现有程序都应该在 Java SE 7 上运行,而无需
修改。然而,有一些小的潜在来源和
JRE 和 JDK 中的二进制不兼容涉及罕见的情况
此处记录的情况和“极端情况”
完整性。

Java SE 7 语言、JVM 或 Java SE API 不兼容

... 以及

JDK 7 和 JDK 6 之间的不兼容性

javac、HotSpot 或 Java SE API 中的 JDK 7 不兼容性

(没有序言 – 只是不兼容性列表。)

Extending answers to include the most recent Java …

Java SE 7 and JDK 7 Compatibility

Quotes from Oracle's undated page:

Compatibility is a complex issue. This document discusses three types
of potential incompatibilities relating to a release of the Java
platform:

  1. Source: Source compatibility concerns translating Java source code into class files including whether or not code still compiles at
    all.
  2. Binary: Binary compatibility is defined in The Java Language Specification as preserving the ability to link without error.
  3. Behavioral: Behavioral compatibility includes the semantics of the code that is executed at runtime.

… and

Incompatibilities between Java SE 7 and Java SE 6 Java SE 7 is strongly compatible with previous versions of the Java platform.
Almost all existing programs should run on Java SE 7 without
modification. However, there are some minor potential source and
binary incompatibilities in the JRE and JDK that involve rare
circumstances and "corner cases" that are documented here for
completeness.

Java SE 7 Incompatibilities in the Language, the JVM, or the Java SE API

… and

Incompatibilities between JDK 7 and JDK 6

JDK 7 Incompatibilities in javac, in HotSpot, or Java SE API

(No preamble there – just a list of incompatibilities.)

小矜持 2024-10-19 11:21:59

仅向后。前向兼容(“优雅地接受用于自身更高版本的输入”)将要求 1.5 JVM 能够运行 1.6 编译的代码,但它不能。

向后要求“如果它可以处理旧设备生成的输入”,这是正确的,因为 1.6 JVM 可以运行 1.5 编译的代码。

JDK/JRE 的每个版本都与 Java 字节码的版本一致。每个编译器都会生成特定字节码版本的代码。每个 JVM 都了解特定字节码版本的一个版本和所有早期版本。

当 JVM 加载一个类时,它会检查字节码版本,如果是 > 则检查字节码版本。比 JVM 最新理解的版本你会得到一个错误。 (ClassVersionError 或其他)。

Backward only. Forward compat ("gracefully accept input intended for later versions of itself") would require the 1.5 JVM to be able to run 1.6 compiled code, which it can't.

Backward requires "if it can work with input generated by an older device" which is true as a 1.6 JVM can run 1.5 compiled code.

Each release of the JDK/JRE coincides with a version of Java bytecode. Each compiler produces code of a specific bytecode version. Each JVM understands a version and all earlier versions of a specific bytecode version.

When the JVM loads a class it checks the bytecode version and if it is > than the JVMs latest understood version you'll get an Error. (ClassVersionError or something).

撕心裂肺的伤痛 2024-10-19 11:21:59

Java (VM) 是向后兼容的。由 java 1.4.2 构建的代码将在 1.5 和 1.5 上运行6 个虚拟机。 JDK 编译器不向后兼容。因此,代码无法由 java 1.5 编译以在 1.4.2 上运行。

Java (VM) is Backward compatible. Code built by java 1.4.2 will run on 1.5 & 6 VM's. The JDK compiler is not backward compatible. So code cannot be compiled by java 1.5 to run on 1.4.2 for example.

风尘浪孓 2024-10-19 11:21:59

JDK 向后兼容,即符合 1.4.2 规范的字节码将在 Java 5 JVM 上运行

JDK is Backward compatible, i.e. Byte Code that complies to 1.4.2 spec will run on Java 5 JVM

戴着白色围巾的女孩 2024-10-19 11:21:59

根据 wiki 的定义,JDK 是向下兼容的。

JDK is downwards compatible as per the definition from wiki.

So尛奶瓶 2024-10-19 11:21:59

它应该向后兼容。

It should be backward compatible.

煮茶煮酒煮时光 2024-10-19 11:21:59

jdk向上兼容——新版本可以在旧版本上运行

jdk is upward compatible - new version can run on old one

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