Are there any specific examples of backward incompatibilities between Java versions?

后端 未结 14 678
孤城傲影
孤城傲影 2020-12-02 12:28

Have there been incompatibilities between Java releases where Java source code/Java class files targeting Java version X won\'t compile/run under version Y (where Y > X) ?

相关标签:
14条回答
  • 2020-12-02 13:02

    Yet another example of java.sql breaking compatibility:

    In 1.5 a compareTo(Date) method was added to java.sql.Timestamp. This method would throw a ClassCastException if the supplied Date was not an instance of java.sql.Timestamp. Of course, java.sql.Timestamp extends Date, and Date already had a compareTo(Date) method that worked with all Dates, so this meant that code that compared a Timestamp to a (non-Timestamp) Date would break at runtime in 1.5.

    It's interesting to note that it appears that 1.6 seems to have fixed this problem. While the documentation for java.sql.Timestamp.compareTo(Date) still says "If the argument is not a Timestamp object, this method throws a ClassCastException object", the actual implementation says otherwise. My guess is that this is a documentation bug.

    0 讨论(0)
  • 2020-12-02 13:04

    First of all, Sun actually considers all of the releases you mentioned (other than 1.0 of course) to be minor releases, not major ones.

    I am unaware of any examples of binary incompatibility in that time. However, there have been some examples of source incompatibility:

    • In Java 5, "enum" became a reserved word; it wasn't before. Therefore, there were source files that used enum as an identifier that would compile in java 1.4 that wouldn't compile in java 5.0. However, you could compile with -source 1.4 to get around this.

    • Adding methods to an interface can break source compatibility as well. If you implement an interface, and then try to compile that implementation with a JDK that adds new methods to the interface, the source file will no longer compile successfully, because it doesn't implement all of the interface's members. This has frequently happened with java.sql.Statement and the other jdbc interfaces. The compiled forms of these "invalid" implementations will still work unless you actually call one of the methods that doesn't exist; if you do that, a MissingMethodException will be thrown.

    These are a few examples I can recall off of the top of my head, there may be others.

    0 讨论(0)
提交回复
热议问题