For long, long time, I have not written code in since if there is a need for the JVM then is my way to go. However, that changes from time to time, specially when I have to work on a shop. Java Scala Java With the new advances on the language, I would say is way easier to work with compare to few years back. There is a lot of movement towards functional programming and it can be seen in the recent additions in the newest versions of . Java Java Java After a lot of efforts to incorporate all these features, there are few that still are a little incompleted. Let’s look at and how to work with it and also how it compares to the . Optional . Scala Option The main idea behind is to avoid nulls and to avoid NullPointerException. However, it feels the one we found in actually promotes the use of nulls. Let’s see how. Optional Java This will throw every time we send a null to it. Instead, we need to use method. NullPointerException .ofNullable Isn’t the main idea to avoid these exceptions? Then why do we get two ways that work so differently from each other and have so different results. In , we only have to do: Scala Note that I am keeping the types at the highest level so everyone could actually be nullable. The point is ,which is the same as , will take care of the null and create the appropriate result for us without throwing NullPointerException which is the exact problem we want to avoid. Option.apply(somethingNull) Option(somethingNull) For our own sake, has the right and and they work as they do in or any other language for that matter. However, makes the signature of these method way more complicated than their ’s counterparts. Java Optional .map .flatMap Scala Java Scala In order words, this is the same as the following in : Scala Note that I am keeping the same generic type names. is worst. .flatMap In we write: Scala In both languages, we could use to extract value from the , however, this operation is discourage since this could end on an exception in both languages. To avoid this, and take different approaches. .get() Optional Java Scala In we need to use to supply a default value if the wrapped value is . Java .orElse null In we use Scala .getOrElse As we can see, both approaches are similar, even though is more concise how to create the value and how it avoids null problems. Scala Optional There is another point where is short, . Java Java Optional is not foldable Every time we put a value inside an optional value at some point we will want to know what kind of value we have in there. Is it null or it is just a regular value? , once again, chooses the verbose, less convenient path. Java Because Option is foldable, we can do this in a more elegant way. Let’s see how. Scala The signature in is the following: .fold Scala Notice that _ifEmpty: => B_ is the same as _Supplier<B>_ in Java. So the question is why they did not add to ? .fold Java Optional For education purpose only, let’s create a in ourselves and see how easy is to have implemented . FoldableOptional Java .fold As we can see here, the implementation cannot be simpler, given the limited toolbox we have in ; still, very straight forward. .fold Java Conclusions Even though the language has been evolving in the last couple of years, it is still far behind of others such as , especially in the functional side. It is very impressive how maintain the same API throughout entire libraries with solid consistency. Even open source libs and projects use the same standards all across. On the other hand, is giving the right steps towards a better (if that actually exist) but there is a long way to go. In the meantime, we have to continue to use the tool on hand to do our daily jobs, so let’s use the good while continue to enhance it. Java Scala Scala Java Java