Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 357
Giorgio Avatar asked Apr 03 '12 19:04

Giorgio


4 Answers

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.

like image 140
Jesper Avatar answered Sep 17 '22 07:09

Jesper


Starting witg Java 1.5 enum became a reserved word. Thus any Java 1.4 source code containing enum became broken starting with 1.5

like image 34
Steve Kuo Avatar answered Sep 21 '22 07:09

Steve Kuo


As far as I know JVMs are backwards compatible. A class compiled with JDK 1 will work in the latest JRE 7. The libraires are definitely not 100% compatible. Some methods have been deprecated (and subsequently removed). Some classes changed behavior in (usually) subtle ways which will cause programs to behave differently.

like image 40
MK. Avatar answered Sep 19 '22 07:09

MK.


You can always run with a newer version of the JDK then the one used for compilation. The other way around is not possible (unless you compile using the -target parameter).

You might want to read this document (in particular the Cross-Compilation Options section), which explains the target parameter and the default behavior

like image 23
Robin Avatar answered Sep 18 '22 07:09

Robin