Are there any specific examples of backward incomp

2019-01-16 09:16发布

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

By "Java release" I mean versions such as:

  • JDK 1.0 (January, 1996)
  • JDK 1.1 (February, 1997)
  • J2SE 1.2 (December, 1998)
  • J2SE 1.3 (May, 2000)
  • J2SE 1.4 (February, 2002)
  • J2SE 5.0 (September, 2004)
  • Java SE 6 (December, 2006)

House rules:

  • Please include references and code examples where possible.
  • Please try to be very specific/concrete in your answer.
  • A class that is being marked as @Deprecated does not count as a backwards incompatibility.

14条回答
Anthone
2楼-- · 2019-01-16 09:39

The semantics of the memory model changed from 1.4 to 1.5. It was changed to allow besides other things double checked locking again. (I think volatile semantics were fixed.) It was broken.

查看更多
神经病院院长
3楼-- · 2019-01-16 09:40

See report on API changes for the JRE class library here: http://abi-laboratory.pro/java/tracker/timeline/jre/

The report includes backward binary- and source-compatibility analysis of Java classes.

The report is generated by the japi-compliance-checker tool.

enter image description here

...

enter image description here

Another interesting analysis for JDK 1.0-1.6 you can find at Japitools JDK-Results page.

查看更多
疯言疯语
4楼-- · 2019-01-16 09:40

As Sean Reilly said, a new method can break your code. Besides the simple case that you have to implement a new method (this will produce a compiler warning) there is a worst case: a new method in the interface has the same signature as a method you do already have in your class. The only hint from the compiler is a warning that the @Override annotation is missing (Java 5 for classes, the annotation is supported for interfaces in Java 6 but optional).

查看更多
闹够了就滚
5楼-- · 2019-01-16 09:41

Obviously the naming convention of release names is not backwards-compatible.

  • JDK 1.0 (January 23, 1996)
  • JDK 1.1 (February 19, 1997)
  • J2SE 1.2 (December 8, 1998)
  • J2SE 1.3 (May 8, 2000)
  • J2SE 1.4 (February 6, 2002)
  • J2SE 5.0 (September 30, 2004)
  • Java SE 6 (December 11, 2006)
  • Java SE 6 Update 10, Update 12, Update 14, Update 16
  • Java SE 7 ??? JDK7?

(The list is from Wikipedia.)

查看更多
手持菜刀,她持情操
6楼-- · 2019-01-16 09:43

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.

查看更多
贪生不怕死
7楼-- · 2019-01-16 09:44

The following will compile under Java 1.4 but not Java 1.5 or later.

(Java 5 introduced 'enum' as a keyword. Note: it will compile in Java 5 if the "-source 1.4" option is provided.)

public class Example {
    public static void main(String[] args) {
        String enum = "hello";
    }
}
查看更多
登录 后发表回答