4

I was looking at the difference between source incompatibilities and behaviorial incompatibilities but I couldn't really understand it. Can someone explain it a little please

If there is a source incompatiblity between Java 8 and Java 7 for example, does that mean that although running Java 7 compiled code with that incompatiblity in Java 8 would work just fine but if I recompile that code with Java 8, I would get an error?

Stuart Marks
  • 127,867
  • 37
  • 205
  • 259
Abby
  • 403
  • 2
  • 6
  • 18
  • java 7 bytecode can run in the java 8 jre. java 8 bytecode cannot run in the java 7 jre – beresfordt Mar 09 '16 at 15:55
  • If there is a source incompatiblity between Java 8 and Java 7 for example, does that mean that although running Java 7 compiled code with that incompatiblity in Java 8 would work just fine but if I recompile that code with Java 8, I would get an error? – Abby Mar 09 '16 at 16:09
  • If you target 1.8 bytecode even if you don't use any java 8 features you would be unable to run that jar in a 1.7 jre – beresfordt Mar 09 '16 at 16:10
  • @beresfordt OP is asking about the opposite direction. – biziclop Mar 09 '16 at 16:11
  • Let's say for example http://bugs.java.com/bugdatabase/view_bug.do?bug_id=7144506 – Abby Mar 09 '16 at 16:13
  • 3
    Definitions of the different kinds of compatibility, in the context of the JDK: https://blogs.oracle.com/darcy/entry/kinds_of_compatibility – Stuart Marks Mar 09 '16 at 16:43
  • Today I wrote [this answer](http://stackoverflow.com/a/35915323/5606016) which is somehow related, it might be interesting having a look at – A_Di-Matteo Mar 10 '16 at 20:17
  • @StuartMarks what's your view on [this](http://stackoverflow.com/questions/35913775/maven-java-version-configuration-ignored-by-eclipse-idea/35915323#35915323) explanation about cross-compilation and common source/target misunderstanding? – A_Di-Matteo Mar 15 '16 at 22:15

1 Answers1

3

Yes, although these cases are really rare, and for good reason.

One obvious example I can think of where this happened in a planned way was when the enum and assert keywords were introduced.

The following would've compiled with Java 1.4:

public void foo() {
   int enum = 42;
}

And would still run on any later JVM, but it wouldn't compile again with a later version of Java. Or to be more procise, it wouldn't compile without specifying the -source 1.4 option to javac.

The compiler bug you cited is an example of how this can happen accidentally (even though the sample code they provided isn't very useful, as even if you manage to compile it, at runtime it will only throw a NPE).

biziclop
  • 48,926
  • 12
  • 77
  • 104