Search code examples
javabackwards-compatibility

Has the Java language maintained source-code backward compatibility throughout its history?


Has Java always maintained source-code backward compatibility during its development?

More precisely: given two Java versions X and Y with X < Y, is any program for Java X also a valid program for Java Y, with the same semantics? E.g. X = Java 2 (or 1.2 with the old numbering) and Y = Java 5.

Or is there only compatibility at the JVM level: e.g. a class compiled for the JVM 1.2 can be run by the JVM 5?

If it is possible to run Java 2 code on a Java 5 (or 6, or 7), what are the exact steps that I have to follow? Compile directly with a Java 5 compiler? Compile with a Java 2 compiler and run on JVM 5?


Solution

  • Sun, and now Oracle, have always been extremely careful with backward compatibility with regards to Java.

    Binary compatibility: You should be able to run Java code compiled with older versions on newer versions without modification. There might, however, be small incompatibilities.

    Source compatibility: Code originally written for an older JDK version should almost always compile without modification with a newer Java compiler, but there are a number of small incompatibilities. One of them is the enum keyword added in Java 5; on older versions of Java, enum was a valid identifier, but not on Java 5. Also, importing classes from the default package has been removed (I think since Java 1.4). So you can't do:

    import SomeClassName;
    

    anymore on Java 1.4 or newer.

    In the documentation of every JDK release there is a document about backward compatibility with previous releases, which lists the details.