Java Reference
In-Depth Information
}
This means any concrete classes of List don't have to explicitly implement sort, whereas in
previous Java versions such concrete classes would fail to recompile unless they provided an
implementation for sort.
But wait a second—a single class can implement multiple interfaces, right? So if you have
multiple default implementations in several interfaces, does that mean you have a form of
multiple inheritance in Java? Yes, to some extent! We show in chapter 9 that there are some
restrictions that prevent issues such as the infamous diamond inheritance problem in C++.
1.5. Other good ideas from functional programming
The previous sections introduced two core ideas from functional programming that are now part
of Java: using methods and lambdas as first-class values, and the idea that calls to functions or
methods can be efficiently and safely executed in parallel in the absence of mutable shared state.
Both of these ideas are exploited by the new Streams API we described earlier.
Common functional languages (SML, OCaml, Haskell) also provide further constructs to help
programmers. One of these is avoiding null by explicit use of more descriptive data types.
Indeed, Tony Hoare, one of the giants of computer science, said in a presentation at QCon
London 2009:
I call it my billion-dollar mistake. It was the invention of the null reference in 1965.... I couldn't
resist the temptation to put in a null reference, simply because it was so easy to implement.
In Java 8 there's an Optional<T> class that, if used consistently, can help you avoid NullPointer
exceptions. It's a container object that may or not contain a value. Optional<T> includes
methods to explicitly deal with the case where a value is absent, and as a result you can avoid
NullPointer exceptions. In other words, it uses the type system to allow you to indicate when a
variable is anticipated to potentially have a missing value. We discuss Optional<T> in detail in
chapter 10 .
A second idea is that of (structural) pattern matching . [ 8 ] This is used in mathematics, for
example:
8 There are two uses of this phrase. Here we mean the one familiar from mathematics and
functional programming whereby a function is defined by cases, rather than using if-then-else.
The other meaning concerns phrases like “find all files of the form 'IMG*.JPG' in a given
directory” associated with so-called regular expressions.
 
Search WWH ::




Custom Search