JDK是“向上”的吗?或“向后”。兼容的?
向后二进制兼容性(或向下兼容性)- 使用旧版本库 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
请注意,要向后兼容的东西必须有一个向前兼容的对应物(有意或无意)。例如:DVD 阅读器是否向后兼容 CD,或者 CD 是否向前兼容 DVD 阅读器?
在这种情况下,这取决于您是否查看编译器(或其生成的字节码)或虚拟机。
该编译器不向后兼容,因为使用 Java5 JDK 生成的字节码无法在 Java 1.4 jvm 中运行(除非使用
-target 1.4
标志进行编译)。但 JVM 是向后兼容的,因为它可以运行较旧的字节码。所以我猜他们选择从javac的角度考虑兼容性(因为它是JDK特有的部分),这意味着生成的字节码可以在jvm的未来版本中运行(与JRE更相关) ,而且还捆绑在 JDK 中)。
简而言之,我们可以说:
(这也是很久以前应该吸取的教训:编写编译器的人通常是对的,而我们使用它们的人是错误的 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:
(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?
扩展答案以包括最新的 Java…
Java SE 7 和 JDK 7 兼容性
引自 Oracle 未注明日期的页面:
… 并且
... 以及
JDK 7 和 JDK 6 之间的不兼容性
(没有序言 – 只是不兼容性列表。)
Extending answers to include the most recent Java …
Java SE 7 and JDK 7 Compatibility
Quotes from Oracle's undated page:
… and
… and
Incompatibilities between JDK 7 and JDK 6
(No preamble there – just a list of incompatibilities.)
仅向后。前向兼容(“优雅地接受用于自身更高版本的输入”)将要求 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).
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.
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
根据 wiki 的定义,JDK 是向下兼容的。
JDK is downwards compatible as per the definition from wiki.
它应该向后兼容。
It should be backward compatible.
jdk向上兼容——新版本可以在旧版本上运行
jdk is upward compatible - new version can run on old one