Java 8 : Introducing functional programming

With the release of Java 8, it has brought functional style of programming to the world. With the introduction of new syntax for lambda expressions and method references, Java 8 has significantly shifted the paradigm of the way and the sytle programmer’s write software applications.

It’s a very exciting time to be in programming world, said Dr. Venkat Subramaniam.

Programming is a way to communicate and functional programming allows you to be highly expressive and convey the intent quickly and flawlessly. It’s a declarative programming paradigm as compared to imperative programming paradigm, which means programming through expressions and declarations rather than statements.

Java 8’s will transform programmers mind and thnking on how to be more expressive and convey the intent easily. We will be using multiple examples to display what Java 8 has to offer.

Java 8 Features:

1. Introduction to functional Programming – Lets take an example of prime number

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

packagecom.java8sample;

import java.util.stream.IntStream;

publicclassPrimeNumber{

publicstaticvoidmain(String[]args){

System.out.println(isPrime(1));

System.out.println(isPrime(2));

System.out.println(isPrime(3));

System.out.println(isPrime(4));

System.out.println();

System.out.println(isPrimeDeclarative(1));

System.out.println(isPrimeDeclarative(2));

System.out.println(isPrimeDeclarative(3));

System.out.println(isPrimeDeclarative(4));

}

/**

* Functional paradigm

*

* @param number

* @return

*/

privatestaticbooleanisPrimeDeclarative(intnumber){

returnnumber&gt;1&amp;&amp;

IntStream.range(2,number)

.noneMatch(index-&gt;number%index==0);

}

/**

* Imperative style paradigm

*

* @param number

* @return

*/

privatestaticbooleanisPrime(intnumber){

for(inti=2;i&lt;number;i++){

if(number%2==0)returnfalse;

}

returnnumber&gt;1;

}

}

In the example above method isPrimeDeclarative shows Java 8 style of functional programming. The focus is more on the iteration rather than implementation of iterations. The code when used declarative uses expressions and describes more of a problem rather than implementation. It expresses what we want to do rather than how to do it.Declarative programming also focuses more on immutability. The code may be less syntactically but humungous semantically. This is a great example and usage of polymorphism. Imperative focuses on how and it involves mutability which is evil in programming.

2. Expressiveness –

Functional programming lets you express yourself and tell story through code. Lets take an example to do this. Example: Find all even number greater than 20 and less than 100;

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

packagecom.java8sample;

import java.util.Arrays;

import java.util.List;

publicclassEvenNumber{

//find even number greater than 20 and less than 100 from a list of numbers.

publicstaticvoidmain(String[]args){

List&lt;Integer&gt;numbers=Arrays.asList(221,2,5,6,21,35,57,322);

//imperative style

for(Integernumber:numbers){

if(number&gt;20&amp;&amp;number&lt;100&amp;&amp;number%2==0){

System.out.println(number);

}

}

//declarative style

System.out.println(numbers.stream().filter(number-&gt;number&gt;20)

.filter(number-&gt;number&lt;100)

.filter(number-&gt;number%2==0)

.findFirst()

.get());

}

}

In this example, the imperative style focuses more on the loop and the logic in the loop where as in declarative style, it uses expressions and allows you to filter with different predicates. Moreover the imperative is a problematic when the desired number is not there in the list. In the declarative if you remove the get() method, you will get an Optional which means that the code will either return empty result or a value. Programmer can completely focus on what and all the implementation is handled by the semantics.

3. Lexical scoping –

Lexical scoping allows us to capture a state.

Lambdas are lexically scoped, meaning that a lambda recognizes the immediate environment around its definition as the next outermost scope

4. Method references

With method references the code becomes more expressive. lets take the same example and use method references to make our code more expressive. We have made it more expressive by using method reference in the filter methods which automatically injects the number into the methods to filter it by the logic expressed in the method itself. This is a feature called higher order function which means we can pass function inside of a function like in javascript, which leads to lazy or asynchronous evaluations.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

packagecom.java8sample;

import java.util.Arrays;

import java.util.List;

publicclassEvenNumber{

//find even number greater than 20 and less than 100 from a list of numbers.

You Might Also Like

Sanjeev Ghimire is passionate about problem-solving through technology. With more than a decade of software engineering experience ranging from fin-tech to healthcare, Sanjeev was also the CTO and co-founder of onTarget. These days, Sanjeev is a Developer Advocate with IBM focused on emerging technologies such as blockchain, IoT, and AR/VR. When not cranking away in Java, Swift, Python, or Scala, you can find Sanjeev at the gym, playing the drums, or catching an Arsenal F.C. (a.k.a. Gunners) soccer match.