0

There are very similar posts to this topic but most of them discuss the fact that you can run a program which was compiled with a later Java version than the JRE's that are running the program. So a program compiled with Java 7 would run with a JRE Java version 8.

My question however is this. Say someone wrote a program a couple of years ago and at that time the latest Java version was Java 5. So they wrote their program using Java 5 features and they compiled their code using Java 5.

Then I come along years later and I want to take their exact code (without changing anything) and recompile it. Can I use Java 8? Or is it safer for me to compile the code using Java 5.

So in short, are there any safety concerns with compiling old code using the newest Java version?

Kyle Bridenstine
  • 6,055
  • 11
  • 62
  • 100
  • 1
    Safer in what way? There is nothing stopping you from compiling the program with java 8 and you certainly should if you intend to run it in a java 8 runtime. – pvg Jan 12 '16 at 20:34
  • 1
    The compilation is pretty much the same, so there shouldn't be any problems. Now the runtime has changed and might have some impact if the application uses some classes in ways it shouldn't (e.g. I saw a bug in one because of the different approach to store Sets on Java 8... but that app was miss using Hashsets) – Augusto Jan 12 '16 at 20:34
  • 1
    Enums were introduced in Java 6. I remember having to change all enum variables to enumeration to get the code to compile. – Gilbert Le Blanc Jan 12 '16 at 20:37
  • 1
    @GilbertLeBlanc Enums were introduced with Java 5. Notice the `@since` tag in the [javadoc for java.lang.Enum](http://docs.oracle.com/javase/8/docs/api/java/lang/Enum.html). – VGR Jan 12 '16 at 22:27
  • 1
    You are _safer_ with Java 5 if you want to make sure everything compiles and runs exactly the same. You are _safer_ with Java 8 if you are concerned about security issues and maintainability. In fact, I think it's quite unpractical to maintain a Java 5 application these days. But if you have a consequent code base, be ready for several days of migration to Java 8. – Didier L Jan 12 '16 at 23:05

1 Answers1

4

Java 8 is backwards compatible, so you should be fine. However it is possible some functions you use are deprecated and in that case you will probably get warnings but still be able to compile and run your code.

You can read more about it over here: Compatibility Guide for JDK 8

  • 2
    There are also some differences in type inference that can cause code to stop compiling. – Louis Wasserman Jan 12 '16 at 20:51
  • 4
    @Mr.Tea not really. There's no good general rule for what fails and what works; type inference is more or less a magic box that usually works and sometimes doesn't. http://stackoverflow.com/q/28466925/869736 has some examples. – Louis Wasserman Jan 12 '16 at 21:08