Friday, November 9, 2018

Java 8 - Lambda Expressions

Greetings!

Before Java8, Java is a fully object oriented programming language. While most of the other programming languages introduces functional programming Java was in OOP way. Java8 introduces lot of features and lambdas are the biggest change that happened to the language so far.

What is functional programming?

It is a programming style that allows to program using expressions. With that we can create instance of a function and assign to a variable, pass functions to methods as arguments.

Is OOP not enough?

We can do everything without lambda expressions. But it help us to write better readable and maintainable codes. And also it gives us better support of multi-cores.
In OOP we are not passing behaviors. We pass objects which have behaviors. In functional programming we can pass behaviors.

Benefits of lambda expressions

  • Parallel processing.
  • Enable functional programming.
  • Readable and concise.

Syntax

A lambda expression has 3 parts.
  • Argument list
  • Arrow token
  • Body

(arguments) -> { body }

  • For a single argument parenthesis are optional.
  • For a single line expressions, brackets are optional.
  • For a single line expressions, return statement can be omitted using brackets.
  • Parameter types are optional. Compiler can inference the type from the value. (type inference)


No comments:

Post a Comment