This week's book giveaway is in the Mac OS forum. We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line! See this thread for details.
In the recent days, I have been working extensively with Scala and I was eagerly looking forward using Java again and Java 8 seems to be promising with a lot of new features. I heard a lot about functional interfaces in Java 8, but never had the time to get into it. What exactly is it? Is it the interface that we write and tell to the compiler through annotations to treat them differently than the traditional interfaces in Java? I would be interested to know more.
SCJP 1.4, SCWCD 1.4 - Hints for you, Certified Scrum Master
Did a rm -R / to find out that I lost my entire Linux installation!
A functional interface is an interface that has only one method. Methods that take the interface as their parameter can then be passed a lambda instead of an object reference.
The @FunctionalInterface annotation is comparable to the @Override annotation. It explains to the compiler what you're trying to do, and the compiler will help you by making sure what you're trying to do is what happens.
If you don't use the @Override annotation, the compiler will not complain if you accidentally end up writing a method that doesn't override the one declared in the super-type. This can lead to problems.
With @FunctionalInterface, you tell the compiler that the interface you're writing should be able to have lambda "instances", instead of traditional anonymous instances. If you accidentally add a second method to the interface, the compiler will complain.
A functional interface will still be a functional interface without the annotation.
For instance, Comparator is now a functional interface. Instead of doing this:
You can now do this:
Actually it's even better to do this: