Search code examples
javacompatibility

When is it soon enough to use new Java features?


I'm developing an application that I plan to publicly release, or at least share with a group of people. The problem that I am facing as a developer is compatibility. On my machine, I am using the new Java 8, but I am afraid to use its new features, and in fact, I'm even afraid to use the new features in Java 7, like try-with-resources, in fear that the users will not have an up-to-date Java version, and consequently, will not be able to use my application. For example, my school's computers still use Java 6.

First off, am I correct in thinking that? Or can code compiled with a newer JDK run on a machine with an older JRE? If I am correct, is there an established "rule" or standard for compatibility? Something like, "Make sure your code is compatible with a JRE that is two versions old!" Or is it purely a matter of the developer's judgement of when new features should be utilized for a released work? And just for emphasis, I am talking about just running the program. The user will never have to compile it.

I know this is a bit open ended, but this is the best place that I could think of to ask. Thanks.


Solution

  • First, you can always specify the version of Java that you want to support by compiling it with the -version flag. This means, since you're using Java 8, you could always force your code to compile down to a lower version (that is, Java 6).

    To your point about older Java versions - yes, you'll likely run into that. A lot of people don't update their Java version for one reason or another. But, this is where you have to make a decision: do you choose to support their version, or do you choose to support another version? This particular part is open-ended, but depending on what you ultimately want to use in your code (diamond operator, try-with-resources (which is actually very nice), NIO), then you'd want to use the version of Java that works well with you and with what you want to support.