Friday, May 02, 2014

Java 8 New Features: Lambda Expressions Part 1

Java is an object-oriented programming language which we all know very well but with Java 8 (released on 18th March, 2014) it goes functional. What does that means? Functional Programming was there way before Java came out but still Java adopted the object-oriented paradigm. Why? Well one simple answer to this is, its popular back then, Why Java wants to adopt Functional Programming now? Well again, simple answer to this question is Modern Hardware now runs more than one CPU Core and as a Programmer we should put them to good use rather then using a single core. Concurrent Programming provides us coarse-grained parallelization that helps Programmers to write code that can put all Cores to good use.

Still accessing a large collection of data is a sequential process well we can make parallel using concurrent programming but thats not an easy task to do at all and even we can't guarantee the performance gains, if any or our data is not in a fragile state or how to design such things that we can reuse it in other projects as well.

If every major Java release since Java 5 adds something to the Java Concurrency API, same way after Java 5, Collections API now makes it easy for accessing the collection of data either sequential or parallel.

Secondly, While doing Event-Driven Programming many of our object instantiation are done via ad-hoc Anonymous Inner Classes and if the code is big the amount of class files it generates with '$' sign is quite a mess. In case, a project deadline is near and there is still a lot of code to write, many developer(s) self question including me "Why the hell we have to write so much boilerplate code and we still achieve very less as compared to other programming languages which runs on the Java Virtual Machine (JVM) like Scala, Groovy etc.. and other which don't run on the JVM but still very less boilerplate code like Python etc.."

As discussed, Java is an object-oriented programming language with exception to having primitives at language level we can either pass an Abstract Data Type (ADT) or Primitives as parameters.
Lets start with a question that we should ask our self before we know whats really changed in Java 8.

Q What are interfaces in Java?

A Very simple answer to this basic question can be  "An interface is 100% Abstract Classes that can only declare abstract methods or may have constant(s)."

If you have plans to move to Java 8 or preparing for an interview etc.., then think once Again?

Interfaces are there since the beginning and even a person who is introduced to Java Programming Language was told the same thing. With Java 8, which is the biggest release after Java 5 has changed things (I won't say a little, you will see it in a bit) so its better to be aware of, what interface were before Java 8 and what they have become Java 8 onward.

So whats really changed in Java 8 with respect to interface?

A new keyword added to the language 'default',
Interface can have default, static and abstract methods,
abstract methods cannot have body but default, static methods have body,
If an interface contains only one abstract method, other than overriding method(s) of Object class as abstract are considered functional interface,
There is a new annotation added in the language, @FunctionalInterface, it's optional, i.e., you can either mark an interface with this annotation or you can leave it, as far as it comply to the definition of @FunctionalInterface, Java treats it as functional interface, whereas, if you mark an interface with the annotation and later on add another abstract method, you will get compilation error.

Things covered so far...

Till Java 7,

lots of boilerplate code,
sequential access to data inside a collection,
Not much of parallel computing (except concurrency API, provides coarse-grained parallelization),
Event-driven Programming is a mess,
Strong typing,
Either we can pass a primitive or ADT as a parameter

Java 8 onward,

concise syntax,
sequential/parallel access to data inside a collection - out-of-the-box,
fine-grained parallelization,
No more Anonymous classes while doing Event-driven programming
type inference,
Now apart from passing primitives or ADT as a parameter, we can do pass lambdas or "code-as-data" as parameters.

Lambda Expressions

Lets now start with an exercise where we can see how things are done till Java 7 and how its improved in terms of syntax, performance etc..