23

At the very end, this article introducing to new Java 8 Optional, states that

Optional is not nearly as powerful as Option[T] in Scala (but at least it doesn’t allow wrapping null). The API is not as straightforward as null-handling and probably much slower. But the benefit of compile-time checking plus readability and documentation value of Optional used consistently greatly outperforms disadvantages

I have a very basic knowledge of Scala and I'm getting familiar with Java 8 Optional, so at a first sight, it's not clear to me what are the differences between the two, if any.

I know, for example, that in Scala I can use pattern matching to test Option and make my life easier. But, excluding what are the features of Scala's syntax, I would like to know if there's something I can do with Option in Scala that I cannot do with Optional in Java.

Hope this is not marked as silly question, but every time I read that 'powerful', question marks fly over my head.

Tagir Valeev
  • 97,161
  • 19
  • 222
  • 334
giampaolo
  • 6,906
  • 5
  • 45
  • 73

2 Answers2

17

If we're talking about differences that aren't syntax-related, Tomasz Nurkiewicz pretty much highlights the biggest one in the opening paragraphs of his blog post:

Optional was introduced in Java 8 so obviously it is not used throughout the standard Java library - and never will be for the backward compatibility reasons.

(emph. mine - although I wouldn't be so adamant on "never", given the new default methods)

So the biggest difference, and the greatest advantage of Scala's Option, seems to be simply that it's much more tightly integrated into the language's API.

First of all, you're very likely to obtain exposure to it, and hence its usage pattern, early on when you start using Scala - most notably, through Scala's Map#get.

And inversely - if you look at Scala's Option API, you will see that it is "spliced" into Scala's collection hierarchy, meaning that you can transparently use it as a collection whenever you need so - e.g. without, say, the end developer of your library ever dealing with its particularities.

Kevin Languasco
  • 2,318
  • 1
  • 14
  • 20
mikołak
  • 9,605
  • 1
  • 48
  • 70
  • Also, there certainly are differences in how the compiler treats typical usage patterns in both languages. For example, when you do pattern match against `Some` or `None` in Scala, the current compiler (2.10.x) will first typecheck via `instanceof` and only then potentially extract the value. Whether this is faster than what `Optional` is doing (probably field loading, comparing to `null` and branching depending on whether it is), w.r.t to JIT, is of course another story. – mikołak Feb 12 '14 at 06:31
  • 2
    I think another factor is Scala, unlike Java greatly discourages the use of null references. That alone would reduce the usefulness of Java's Optional in real app I think... – seand Feb 12 '14 at 08:05
  • @seand: yup, that's pretty much one of the implications (or, more likely, causes) of "`Option` being integrated into the API". But I don't think anyone in their right mind would use Java's `Optional` in Scala code, if that's what you mean. – mikołak Feb 12 '14 at 08:15
  • 1
    When you code in Scala it's pretty safe to assume a given variable isn't null (unless you're interfacing with Java). So, Option is a useful mechanism for indicating "hey this might not have a value, you better be able to deal with it...". But in Java8 that might not always apply. – seand Feb 12 '14 at 08:20
  • @TheTerribleSwiftTomato: if I understand correctly, the problem is not the class itself, but the available libraries. Option and Optional have the same semantic. – giampaolo Feb 12 '14 at 21:32
  • 3
    @giampaolo : in essence, yes. The *basic* semantics are mostly the same, Java's `Optional` is even monadic (`of ~= unit`, `flatMap ~= bind`). The "power" comes from the integration with and usage in the respective languages' standard libraries. It might not seem like a big deal at a glance, but has far-reaching consequences - seand's last comment is spot-on in that regard. – mikołak Feb 12 '14 at 22:07
0

I would like to add to the previous answer that apart from Java Optional not being integrated into standard java library (and other libraries of course), there is another big difference.

Java Optional is not Serializable and as such cannot be used in fields of any Java Beans which should be serializable.

Java Optional spec: https://docs.oracle.com/javase/8/docs/api/java/util/Optional.html

Scala Option spec: https://www.scala-lang.org/api/2.12.x/scala/Option.html