Hi Everyone, welcome back to this tutorial. In this tutorial we are going to code a simple example to
showcase the difference between the imperative style of programming and declarative style of programming.
Use-case is perform the summation of 100 numbers from 0 to 100 and display the result.
Let's go ahead and code this one. I have the IntelliJ open here.
I don't want you guys to feel odd that I jumped in to the code directly without explaining about setting up the
workspace.
This is just for the demo purpose,
that's the reason why I jumped into the code directly.
We have a separate workspace set up section and the actual course is going to walk you through the project
set up, creating classes step by step.
Now let's go ahead and code this one.
I'm going to create a class called Imperative versus Declarative example one and I'm going to make
this classic executable by adding public static void main method.
Let me put this in presentation mode.
The first thing is we're going to code this use case using the imperative style.
Let me give some code comments here so that we can differentiate between the two.
This is the how style of programming ?.
followed by that we are going to code the declarative style of programming.
Basically this is the what style of programming.
You'll be able to understand the difference between them by the end of this tutorial.
Now the use case is to perform the summation of first hundred numbers from 0 to 100.
I'm going to create a variable int sum=0;
Basically I'm going to store the result in this sum int variable.
Now I'm going to create a for loop int i=0;
So we're going to perform the summation of the first hundred numbers. Right ?
Now, we need to iterate for the first hundred times and the variable is going to be the value is going
to be 100.
Followed by that we're going to have the increment operator.
So every time this loop iterates we are going to increase the sum by the i value.
Now the final result is going to be.
Sum using imperative approach. Going to be sum. Now lets run this program and check the result.
So , the result is 5050. This is the how style of programming.
Now I'm going to make use of the functions that got introduced as part of the Java8 in order to code
the same use case.
What I'm going to do is Intstream, there is a Intstream class that got introduced as part of Java 8 and
then I'm going to give the range. The range is going to be zero to 100 .
So when I give rangeClosed. Basically this is going to include the 100 value also.
and then press Dot this is going to give you the access to the sum method.
This sum method is going to give you the result which is the summation of all the values from 0
to 100.
Now I'm going to print this value here using declarative approach.
approach.
This is going to be sum1. Lets run this and check the result. There you go. It gave you the same result.
Now let's compare this approach with this approach. let's start with the imperative approach. Here.
we defined the summation needs to be done right.
The first step is we declared a variable and then be we coded for loop and defined how many times
this for loop needs to iterate and we explicitly increment the value of summation and get to the result.
This is way we have been coding since the existence of Java and it has a lot of drawbacks to it.
Like if you take a look at it we have a mutable variable.
Here we are mutating the variables in order to get to the result.
If we try to run this code in a multi-threaded environment we are going to have a lot of problem with
this code.
You might get unexpected results.
And on the code readability perspective you need to go through the code line by line in order to understand
what's happening. Here
we have a simple logic but in reality some use cases can lead to make you write complex code.
Now let's take a look at the declarative approach.
This is what style of programming meaning you ask for what you want but you don't care about how the
research is provided to you.
If you take a look at the code.
Here we have provided the range right.
It performs something called internal iteration instead of the external iteration.
If we compare this code with this code this performs ex-general iteration because we are explicitly
iterating the for loop of from 0 to 100 range. It performs a summation behind the scenes and give you
the result as soon as you call that sum method.
But we don't have to worry about how would perform the summation behind the scenes.
This piece of code will run us expected even in multi-threaded environment. Lets say you want to run this code
in a multi threaded environment.
Basically you have to create the threads and you have to start the thread in order to make this piece
of logic to be run in a multi-threaded environment.
But with this code if I have to run this code in a multi-threaded I just have to call the parallel
in it.
It splits the values which is a source Right?.
It's splits the values and perform the summation and gives you the result. You might be wondering how many threads
are created , how it works internally.
All these concepts are covered in the course as part of the parallel programming section.
It might be confusing now you just have to get into some hands on in order to be clear with these
new classes and concepts. With this what I can say is that we can write better code with Java 8 features.
We came to the end of the tutorial. Let's discuss another use case in the next tutorial.
Thank you for watching.