且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2022-05-15 00:33:29

请注意,要使某些东西向后兼容,必须有一个对向兼容的对象(有意或无意).例如:DVD阅读器向后兼容CD还是CD向前兼容DVD阅读器?

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.

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

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.

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

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(通常)是向前兼容的.
  • JRE(通常)是向后兼容的.

(这也是很久以前应该学习的一课:编写编译器的人通常是正确的,而我们使用它们的人是错误的xD)

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