66

When answering a Java question where a specific Java version is not mentioned and the code does not tell which version the poster uses I often hesitate to use Java 8 constructs. Even though Java 8 has been live for quite a while I get the feeling that many developers are not used to the new constructs and have a harder time understanding my answers if I start throwing in some streams and lambdas.

How should I think in this case? Should I stop hesitating and go all-in on Java 8?

8
  • 23
    My opinion answer with new version always helpful. Commented Jun 7, 2015 at 17:05
  • 7
    You could leave a comment asking the Asker if they need an answer for a specific version of Java and/or point out in your answer that you're using Java 8.
    – BSMP
    Commented Jun 7, 2015 at 18:36
  • 17
    "many developers are not used to the new constructs" -> that's not a reason to hold off; quite the reverse. Commented Jun 8, 2015 at 4:33
  • 15
    I usually preface with: "If you use Java 8, ..."
    – PM 77-1
    Commented Jun 8, 2015 at 5:04
  • 3
    You learn to should stop worrying and love the bomb (Java 8 in this case). So, go all for it. Commented Jun 8, 2015 at 11:18
  • 4
    Also remember that your answer is not only for the OP but also for any future readers, therefor an answer using a recent version will be longer useful.
    – LisaMM
    Commented Jun 9, 2015 at 12:05
  • 1
    lambdas, yes. it shortens the code and enhance readability (in lots of cases, but not all) (also you can port back lambdas to java7). Streams and stuff are more specific, and everyone may not be familiar with them, even when java8 is the mainly used version. Offer 2 options when possible (?).
    – njzk2
    Commented Jun 9, 2015 at 13:30
  • There's still people that sometimes "get angry" or start commenting when I post python3 code, which has been around more than 5 years now... Sorry dude, if you expect me to write python2 code or python2 compatible code be clear and state in your question that you are using an outdated version of python via a version-specific tag... (sorry for the intrusion, but the issue with Java8 is similar, and I believe the python3 case is much worse).
    – Bakuriu
    Commented Jun 9, 2015 at 20:48

8 Answers 8

78

If it makes sense to use a Java 8 feature, I say go for it. Java 8 will eventually become the standard Java that "everybody" uses, so questioners might as well learn how to use it right now. It's probably worth making a note that it requires Java 8, otherwise you might get spammed with "y dis no work" comments.

You can also create two code samples that solve the problem, one in 7 and one in 8.

7
  • 16
    In addition, I would probably add a sentence like "If you use Java 8, you can use the following..."
    – user12205
    Commented Jun 7, 2015 at 22:00
  • 20
    Also the OP need not accept the answer using the java 8 features, but it may be useful for other people.
    – fabian
    Commented Jun 7, 2015 at 22:07
  • 8
    Might also want to mention that Java7 is EOL now, there'll be no more public updates for JDK 7 so (most) people should really make an effort to migrate.
    – ivarni
    Commented Jun 8, 2015 at 4:51
  • 3
    @ivarni End of public support isn't quite the same as EOL (see extended support date in your link). In practice, a number of places will not be able to upgrade to more recent versions for quite a while (unfortunately).
    – Bruno
    Commented Jun 8, 2015 at 16:56
  • 7
    I'm a zealot about new versions of things so this is biased, but if a note is warranted, I suggest putting it at the end of an answer, e.g. "by the way, this will/may not work with Java 7, which is no longer current". Forge ahead boldly and assume the new version, warn people about possible shortcomings of the old. Encourage the forward-looking state of mind. Commented Jun 8, 2015 at 20:45
  • 3
    @Leushenko The problem is that what's "current" is relative. Firstly, you can have multiple supported versions at the same time (as it's the case for Java, at least before the end of public support). Secondly, anyone with a bit of experience in the field will have been in situations where you can't upgrade just like that, e.g. someone has a policy where they need to go through hoops to deploy a new version, or there already is code running on the past version that may break if it's upgraded (there's always a risk that needs to be assessed, and potential problems, which might not be solvable).
    – Bruno
    Commented Jun 9, 2015 at 12:19
  • I think clojure will replace java 8 and java 8 will never become a standard.
    – emory
    Commented Jun 11, 2015 at 1:24
25

Tread with caution. There are key differences between Java 7 and Java 8 that don't just involve lambda support which will trip up and confuse an asker.

Take, for example, this particular question. The code above compiles just fine in Java 8, but will not compile in Java 7 due to a change in how final and effectively final variables are recognized. If you were using pure Java 8, you wouldn't be able to identify this particular problem.

It is also of the utmost importance to consider that not everyone has made the switch to Java 8. Solutions written exclusively in that language's vernacular will not be of any use to anyone using Java 7.

3
  • 1
    Maybe we can take inspiration from how the Python guys approached the same problem when the 2-to-3 transition occurred. Martijn? :) Commented Jun 7, 2015 at 17:26
  • 1
    I'd say that's a wee bit different. Java 7 code will compile just fine in Java 8, but Java 8-specific features and nuances won't translate well to Java 7. It's a graduated transition as opposed to, "This is what you need to do to make sure your program will compile..."
    – Makoto
    Commented Jun 7, 2015 at 17:29
  • 8
    So it's not that different from, say, C++11 versus C++98? How come we don't already have a canonical answer for this, then? I only found this (more related but closed as a dupe) on the Overmeta. Commented Jun 7, 2015 at 17:31
17

I'd like to call out the special case of adding a Java 8 answer to an old question, which can be very helpful. Java 8 (and new versions of programming languages in general) addressed some pain points of previous versions, and many operations that used to require Guava can now be concisely performed with the standard library classes. New answers to old questions help readers avoid taking unnecessary dependencies/using unnecessary language workarounds, and are very helpful (and often well-rewarded).

12

My personal rule of thumb is that if the question does not specify any version constraints, I always assume the latest version. In fact, I define "latest version" to include public betas as well.

In addition, if there is a version in development but not yet in public beta, that has some new features which would significantly impact the complexity of the solution, I would mention that as well in a separate paragraph.

I realize that the Java world is generally slower to adopt new versions than the Ruby world, where I mostly hang out. However, Oracle is significantly more aggressive now than Sun was. (Java 7 had the shortest support period of any Java version released in this century.) Java 7 is already EOL, and Oracle has started rolling out Java 8 as an automatic upgrade to existing Java 7 installations (something which probably would have been unthinkable during the Sun era).

In short: in general, always use the latest version. Specifically with Java, there is no reason to not adhere to this general rule.

1
11

If the question is being asked from a "how do data structures work" perspective, and you answer the question using a Java 8 construct that essentially does the data structure work for you but hides the details of the data structure's inner workings, then you're not really answering the question, are you?

On the other hand, if you're answering a question that is language-agnostic, a Java Stream or lambda expression seems a bit specific. Very likely someone asking a language-agnostic question is looking for something a bit more broadly applicable than that. For language-agnostic questions, think more in terms of rudimentary C, Python, Javascript or pseudocode, as the question is more likely algorithmic in nature.

In general, if the question isn't even tagged Java, I would avoid features that require knowledge about a specific version of Java.

1
  • 4
    I'm not sure but the way I read the OPs question, it sounded more like "Java specific question that didn't bother indicating specific Java version" and not "language agnostic"
    – DVK
    Commented Jun 9, 2015 at 13:11
6

If the version is not specified, I would assume the latest version, or a reasonably recent version commonly in use, but make it clear I'm using new features when this is the case (and if I'm aware they're new features).

In addition, it's never a bad idea to point to the release notes that support the introduction of those new features (if applicable). For example, links to Java SE 7 Security Enhancements or Java SE 8 Security Enhancements can often improve answers to give a bit of background (assuming the question is about some security feature and Java, of course).

Often, it's also worth giving a hint regarding how to address the issue with a previous version (or add an alternative solution for a previous version as part of the same answer). When it comes to Java, unless explicitly specified in the question, I would at least assume Java 7 and wouldn't go back any further than Java 6, though (at the moment). (Unfortunately, it's still in use, and some people are stuck in situations where they can't upgrade for one reason or another.)

That said, if you're unable or unwilling (that's not a reproach) to provide a complete answer that targets multiple reasonably recent versions, assume the latest version or a version that's likely to be commonly in use at the time you write your answer. Remember that, while Stack Overflow only has room for one accepted answer, there's nothing wrong with other users providing good answers too, which may be relevant in a slightly different context (e.g. different versions).

It only becomes tricky if you have to say "You can't do this in Java" when you can do whatever is asked with Java 8. It helps to say "As of Java 7, you can't do this in Java" if you're not sure of what has changed.


Java has a reasonably well-managed release cycle. This could be more problematic for other platforms or libraries that change very frequently. There are some trendy open source libraries and frameworks out there that are developed at a rapid pace, making current solutions inapplicable for 6-month old releases. In that case, just ask the OP which version they're using and whether they would be able to upgrade (if that makes a significant difference).

3

Similar problems are occurring in C++ land, where C++11 feature compliance is not universal, C++14 is spotty, and C++1z (std::experimental and other) features are on the horizon.

My personal approach at this time is to answer using C++14 and mention what C++14 features I'm using, with at least a sentence to describe how to replace them. I won't implement the replacement, but I will give enough keywords that someone can learn how to do it.

If C++1z has a feature that would make the solution better, I'll try to mention it. I try not to rely on it in answers without prompting.

A year or so ago, I'd default to C++11, and if I used C++14 library features I'd either re-implement them (many of them are 2-liners), and if I wanted the language feature I'd just mention that it would make things easier.

A year or two before that, I'd be cautious about using C++11 at all, and try to do it in C++03. If I used C++11, I'd mention it explicitly, and sometimes implement it manually in C++03 if needed.

Before that, I would mention C++11 as in "you can do this better in C++11 via $some_keyword$."

In short, it is a gradient. It moved as I found people where less and less likely to say "I cannot get this to work on my compiler, I cannot use C++11" in response to my use of the features.

If you are worried, start using Java 8, mention explicitly you are using it, and (optionally) include a short description of what Java 7 changes would be required. If lots of people are stuck on Java 7, they will say "I cannot get it to work on Java 7, can you help?" in your comments. And you'll learn you moved to Java 8 by-default a bit too fast.

1

If there is overlap in the solution to a problem between one version of a language and another then it is advisable to either show both solutions, or only the solution which works with the largest use of the language.

This situation isn't limited to Java. Most of the popular languages on the exchange have gone through these types of updates while Stack Overflow has been around, some multiple times already. For example, the changes in c#, JavaScript, python, C++, etc.

There are a few caveats to this scenario though.

  • The question's topic is regarding the cutting edge technology (Java 8 in this case)

    In this scenario, the solution really must contain the new approach using the new technology. It benefits everyone to have solutions available to cutting edge technology and it is definitely part of the scope of the question.

  • The question's topic is regarding a generic approach using the technology

    If the cutting edge technology is not mentioned, then it should not be the only solution offered. In fact, it is probably not required to be included at all if there is a solution using existing approaches. Include the existing approach as a base, and if so inclined the cutting edge technology may also be included as a sort of addendum for future visitors.

  • The question's topic is related to legacy or the question was asked a decent amount of time ago

    The cutting edge version really has no place here. These types of questions are specifically addressing concerns where the cutting edge technology is most likely not an option because of reasons. Avoid placing the cutting edge solution in this type of question.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .