WEBVTT

0
00:02.020 --> 00:06.740
Hi Everyone,  welcome back to this tutorial. In this tutorial  we are going to code a simple example to

1
00:06.740 --> 00:12.110
showcase the difference between the imperative style of programming and declarative style of programming.

2
00:12.370 --> 00:18.940
Use-case is  perform the summation of 100 numbers from 0 to 100 and display the result.

3
00:19.130 --> 00:22.950
Let's go ahead and code this one. I have the IntelliJ open here.

4
00:22.970 --> 00:28.250
I don't want you guys to feel odd that I jumped in to the code directly without explaining about setting up the

5
00:28.250 --> 00:28.820
workspace.

6
00:28.820 --> 00:30.900
This is just for the demo purpose,

7
00:30.950 --> 00:33.290
that's the reason why I jumped into the code directly.

8
00:33.350 --> 00:38.700
We have a separate workspace set up section and the actual course is going to walk you through the project

9
00:38.700 --> 00:42.250
set up, creating classes step by step.

10
00:42.250 --> 00:44.230
Now let's go ahead and code this one.

11
00:44.230 --> 00:56.770
I'm going to create a class called Imperative versus Declarative example one and I'm going to make

12
00:56.770 --> 01:01.380
this classic executable by adding  public static void main method.

13
01:01.500 --> 01:03.530
Let me put this in presentation mode.

14
01:03.700 --> 01:07.460
The first thing is we're going to code this use case using the imperative style.

15
01:08.140 --> 01:11.780
Let me give some code comments here so that we can differentiate between the two.

16
01:11.800 --> 01:14.020
This is the how style of programming ?.

17
01:16.910 --> 01:21.070
followed by that we are going to code the declarative style of programming.

18
01:25.970 --> 01:29.140
Basically this is the what style of programming.

19
01:30.170 --> 01:33.900
You'll be able to understand the difference between them by the end of this tutorial.

20
01:33.950 --> 01:39.660
Now the use case is to perform the summation of first hundred numbers from 0 to 100.

21
01:39.680 --> 01:43.380
I'm going to create a variable int sum=0;

22
01:43.580 --> 01:47.670
Basically I'm going to store the result  in this sum int variable.

23
01:47.720 --> 01:52.270
Now I'm going to create a for loop int i=0;

24
01:52.280 --> 01:55.390
So we're going to perform the summation of the first hundred numbers. Right ?

25
01:55.390 --> 02:01.430
Now, we need to iterate for the first hundred times and the variable is going to be the value is going

26
02:01.430 --> 02:02.270
to be 100.

27
02:02.600 --> 02:06.530
Followed by that we're going to have the increment operator.

28
02:06.760 --> 02:13.970
So every time this loop iterates we are going to increase the sum by the i value.

29
02:14.120 --> 02:18.140
Now the final result is going to be.

30
02:18.260 --> 02:31.430
Sum using imperative approach. Going to be sum. Now lets run this program and check the result.

31
02:34.610 --> 02:39.370
So , the result is 5050. This is the how style of programming.

32
02:39.420 --> 02:47.370
Now I'm going to make use of the functions that got introduced as part of the Java8 in order to code 

33
02:47.370 --> 02:48.490
the same use case.

34
02:48.630 --> 02:55.920
What I'm going to do is Intstream,  there is a Intstream class that got introduced as part of Java 8 and

35
02:55.920 --> 03:01.440
then I'm going to give the range. The range is going to be zero to 100 .

36
03:01.660 --> 03:07.190
So when I give rangeClosed. Basically this is going to include the 100 value also.

37
03:08.130 --> 03:13.880
and then press Dot this is going to give you the access to the sum method.

38
03:14.520 --> 03:23.730
This sum method is going to give you the result which is the summation of all the values from 0

39
03:23.730 --> 03:24.710
to 100.

40
03:24.720 --> 03:28.560
Now I'm going to print this value here using declarative approach.

41
03:32.050 --> 03:32.540
approach.

42
03:32.550 --> 03:39.770
This is going to be sum1. Lets run this and check the result. There you go. It gave you the same result.

43
03:39.910 --> 03:48.130
Now let's compare this approach with this approach. let's start with the imperative approach. Here.

44
03:48.170 --> 03:50.830
we defined the summation needs to be done right.

45
03:50.910 --> 03:58.140
The first step is  we declared a variable and then be we coded  for loop and defined how many times

46
03:58.140 --> 04:05.370
this for loop needs to iterate and we explicitly increment the value of summation and get to the result.

47
04:05.400 --> 04:11.240
This is way we have been coding since the existence of Java and it has a lot of drawbacks to it.

48
04:11.280 --> 04:14.400
Like if you take a look at it we have a mutable variable.

49
04:14.400 --> 04:19.170
Here we are mutating the variables in order to get to the result.

50
04:19.350 --> 04:24.800
If we try to run this code in a multi-threaded environment we are going to have a lot of problem with

51
04:24.800 --> 04:25.740
this code.

52
04:25.770 --> 04:28.210
You might get unexpected results.

53
04:28.480 --> 04:34.560
And on the code readability perspective you need to go through the code line by line in order to understand

54
04:34.560 --> 04:35.740
what's happening. Here

55
04:35.750 --> 04:42.780
we have a simple logic but in reality some use cases can lead to make you write complex code.

56
04:42.780 --> 04:45.290
Now let's take a look at the declarative approach.

57
04:45.300 --> 04:51.390
This is what style of programming meaning you ask for what you want but you don't care about how the

58
04:51.390 --> 04:53.160
research is provided to you.

59
04:53.220 --> 04:54.500
If you take a look at the code.

60
04:54.500 --> 04:56.720
Here we have provided the range right.

61
04:56.730 --> 05:01.890
It performs something called internal iteration instead of the external iteration.

62
05:01.890 --> 05:08.010
If we compare this code with this code this performs ex-general iteration because we are explicitly

63
05:08.010 --> 05:17.070
iterating the for loop of from 0 to 100 range. It performs a summation behind the scenes and give you

64
05:17.070 --> 05:19.460
the result as soon as you call that sum method.

65
05:19.620 --> 05:24.660
But we don't have to worry about how would perform the summation behind the scenes.

66
05:24.720 --> 05:31.290
This piece of code will run us expected even in multi-threaded environment. Lets say you want to run this code

67
05:31.290 --> 05:32.840
in a multi threaded environment.

68
05:32.880 --> 05:37.950
Basically you have to create the threads and you have to start the thread in order to make this piece

69
05:37.950 --> 05:41.400
of logic to be run in a multi-threaded environment.

70
05:41.640 --> 05:48.250
But with this code if  I have to run this code in a multi-threaded  I just have to call the parallel

71
05:48.300 --> 05:48.860
in it.

72
05:48.870 --> 05:52.320
It splits the values which is a source Right?.

73
05:52.470 --> 05:58.080
It's splits the values and perform the summation and gives you the result. You might be wondering how many threads

74
05:58.080 --> 06:00.260
are created , how it works internally.

75
06:00.270 --> 06:05.340
All these concepts are covered in the course as part of the parallel programming section.

76
06:05.370 --> 06:11.310
It might be confusing now you just have to get into some hands on in order to be clear with these

77
06:11.310 --> 06:18.510
new classes and concepts. With this  what I can say is that we can write better code with Java 8 features.

78
06:18.690 --> 06:23.170
We came to the end of the tutorial. Let's discuss another use case in the next tutorial.

79
06:23.190 --> 06:24.320
Thank you for watching.