spring-5-with-spring-boot-2 / 02 - Core Java /012 Arithmetic Operators_en.srt
Tan115's picture
Add files using upload-large-folder tool
78b4844 verified
Raw
History Blame Contribute Delete
21.5 kB
1
00:00:00,000 --> 00:00:03,000
(piano music)
2
00:00:03,000 --> 00:00:05,000
-: In this video, let's talk about operators.
3
00:00:05,000 --> 00:00:06,000
That's right.
4
00:00:06,000 --> 00:00:08,000
You can perform operations on different values.
5
00:00:08,000 --> 00:00:09,000
In fact, we have done that before,
6
00:00:09,000 --> 00:00:13,000
but let's expand the examples and more operators.
7
00:00:13,000 --> 00:00:15,000
So, basically we can add two numbers,
8
00:00:15,000 --> 00:00:16,000
we can subtract two numbers,
9
00:00:16,000 --> 00:00:17,000
we can divide two numbers,
10
00:00:17,000 --> 00:00:19,000
we can multiply two numbers
11
00:00:19,000 --> 00:00:21,000
and also we can perform an operation,
12
00:00:21,000 --> 00:00:25,000
so that you will get a remainder of an operation.
13
00:00:25,000 --> 00:00:26,000
Okay, so when you divide a number
14
00:00:26,000 --> 00:00:29,000
and if you only want a remainder, we can do that as well.
15
00:00:29,000 --> 00:00:32,000
So, let me show you those things with code here.
16
00:00:32,000 --> 00:00:33,000
So, what I will do now is first of all,
17
00:00:33,000 --> 00:00:34,000
let's create two variables
18
00:00:34,000 --> 00:00:36,000
and of course we can name any variable name.
19
00:00:36,000 --> 00:00:39,000
Let's go with integer num1,
20
00:00:39,000 --> 00:00:41,000
and the value, it will have, let's say seven.
21
00:00:41,000 --> 00:00:43,000
And then let's have one more variable here,
22
00:00:43,000 --> 00:00:46,000
which is, let's say five, num2 is five.
23
00:00:46,000 --> 00:00:48,000
And then I can perform some operation
24
00:00:48,000 --> 00:00:51,000
and I can save that operation in a result.
25
00:00:51,000 --> 00:00:54,000
So let's say if you want to add two numbers, we can do that.
26
00:00:54,000 --> 00:00:56,000
We can save the output in result,
27
00:00:56,000 --> 00:00:58,000
and then to perform the operation,
28
00:00:58,000 --> 00:00:59,000
we can use a plus operator.
29
00:00:59,000 --> 00:01:02,000
That's right, we can actually use a plus operator
30
00:01:02,000 --> 00:01:03,000
to add two numbers.
31
00:01:03,000 --> 00:01:05,000
And then once you have done that,
32
00:01:05,000 --> 00:01:06,000
we can simply come back here.
33
00:01:06,000 --> 00:01:09,000
We can say system dot O, dot print, Ellen,
34
00:01:09,000 --> 00:01:11,000
and here we can print the result
35
00:01:11,000 --> 00:01:13,000
and just to see if it is working.
36
00:01:13,000 --> 00:01:14,000
And as we discussed before,
37
00:01:14,000 --> 00:01:16,000
this is not a good way of running a code.
38
00:01:16,000 --> 00:01:18,000
You have to first compile the code, then run.
39
00:01:18,000 --> 00:01:22,000
But we got a shortcut, I can simply use Java space file name
40
00:01:22,000 --> 00:01:24,000
with J, with dot Java.
41
00:01:24,000 --> 00:01:26,000
Say Enter, and we got 12.
42
00:01:26,000 --> 00:01:30,000
So when you say seven plus five, we got 12, cool.
43
00:01:30,000 --> 00:01:33,000
Now what I will do is, so we can add two numbers.
44
00:01:33,000 --> 00:01:34,000
What about supply action?
45
00:01:34,000 --> 00:01:34,000
Let's try that.
46
00:01:34,000 --> 00:01:37,000
So I will say minus symbol and run.
47
00:01:37,000 --> 00:01:38,000
You can say we got two.
48
00:01:38,000 --> 00:01:39,000
So just working.
49
00:01:39,000 --> 00:01:40,000
So we can add two numbers,
50
00:01:40,000 --> 00:01:42,000
we can supply two numbers.
51
00:01:42,000 --> 00:01:43,000
How about multiplication?
52
00:01:43,000 --> 00:01:44,000
We can do that.
53
00:01:44,000 --> 00:01:47,000
So we can say star num1 into num2.
54
00:01:47,000 --> 00:01:48,000
Come back here.
55
00:01:48,000 --> 00:01:50,000
Run, you can see we got 35.
56
00:01:50,000 --> 00:01:51,000
So seven into five is 35.
57
00:01:51,000 --> 00:01:52,000
That work, that is working.
58
00:01:52,000 --> 00:01:54,000
How about division?
59
00:01:54,000 --> 00:01:55,000
Let's try that.
60
00:01:55,000 --> 00:01:56,000
So the moment I said division, we have
61
00:01:56,000 --> 00:01:57,000
to use a slash.
62
00:01:57,000 --> 00:01:59,000
Okay, so we don't have a division
63
00:01:59,000 --> 00:02:00,000
in fact, that's how we do, right?
64
00:02:00,000 --> 00:02:03,000
We underscore or something with a slash.
65
00:02:03,000 --> 00:02:07,000
So yeah, we got slash, let's try and you can see we got one.
66
00:02:07,000 --> 00:02:08,000
That's right.
67
00:02:08,000 --> 00:02:09,000
One is a question here, right?
68
00:02:09,000 --> 00:02:12,000
So when you divide seven by five, you get two things.
69
00:02:12,000 --> 00:02:14,000
One is a question, which is one,
70
00:02:14,000 --> 00:02:16,000
and then you will also get a remainder.
71
00:02:16,000 --> 00:02:18,000
Now in this case, when you use slash,
72
00:02:18,000 --> 00:02:21,000
you will only get a question, not a remainder.
73
00:02:21,000 --> 00:02:22,000
So how will you get a remainder?
74
00:02:22,000 --> 00:02:25,000
In that case you have to use a modulus operator.
75
00:02:25,000 --> 00:02:28,000
So we can use, when you say percentage symbol like this,
76
00:02:28,000 --> 00:02:30,000
it is a modus operator that
77
00:02:30,000 --> 00:02:33,000
what it will give you is only a remainder.
78
00:02:33,000 --> 00:02:36,000
So if I run this code, you can see we got two,
79
00:02:36,000 --> 00:02:37,000
two's a remainder here right.
80
00:02:37,000 --> 00:02:39,000
Now We can use any complex numbers as well,
81
00:02:39,000 --> 00:02:41,000
whatever number you want to perform with,
82
00:02:41,000 --> 00:02:43,000
and I would suggest you to try a certain example so
83
00:02:43,000 --> 00:02:46,000
that you'll get the, get used to this operations.
84
00:02:46,000 --> 00:02:48,000
Now all this operation actually makes sense, right?
85
00:02:48,000 --> 00:02:50,000
These are very simple stuff to understand.
86
00:02:50,000 --> 00:02:52,000
Can we use some other operators as well?
87
00:02:52,000 --> 00:02:53,000
Let's try.
88
00:02:53,000 --> 00:02:55,000
What I will do now is let me just remove this result
89
00:02:55,000 --> 00:02:57,000
for time being, or maybe I can just comment this part.
90
00:02:57,000 --> 00:03:00,000
I also don't need this num2, let me only focus on the
91
00:03:00,000 --> 00:03:02,000
first variable, which is num1.
92
00:03:02,000 --> 00:03:06,000
Now, let's say I want to add this num1 by two.
93
00:03:06,000 --> 00:03:09,000
So basically the value of num1 is seven.
94
00:03:09,000 --> 00:03:11,000
I want to increment the value by two.
95
00:03:11,000 --> 00:03:14,000
So what we can do is we can say num1 is equal to.
96
00:03:14,000 --> 00:03:16,000
So basically we have to use num1,
97
00:03:16,000 --> 00:03:18,000
which is num1 in this case,
98
00:03:18,000 --> 00:03:21,000
and we'll add a value to it, which is two.
99
00:03:21,000 --> 00:03:23,000
And then whatever new value you will generate
100
00:03:23,000 --> 00:03:25,000
after this will be assigned to num1.
101
00:03:25,000 --> 00:03:27,000
Okay, that looks cool, right?
102
00:03:27,000 --> 00:03:28,000
Let's try this.
103
00:03:28,000 --> 00:03:30,000
I will say num1, between num1,
104
00:03:30,000 --> 00:03:31,000
and let's run this code.
105
00:03:31,000 --> 00:03:32,000
You can see we got nine.
106
00:03:32,000 --> 00:03:33,000
So this is working,
107
00:03:33,000 --> 00:03:34,000
so seven became nine.
108
00:03:34,000 --> 00:03:36,000
So just working right.
109
00:03:36,000 --> 00:03:36,000
Now when you know
110
00:03:36,000 --> 00:03:40,000
that you are adding the number by itself, okay,
111
00:03:40,000 --> 00:03:44,000
what you can do is you can just use a shortcut.
112
00:03:44,000 --> 00:03:45,000
Now what that shortcut says is,
113
00:03:45,000 --> 00:03:47,000
let me just comment this section here
114
00:03:47,000 --> 00:03:49,000
and let's write it fresh here.
115
00:03:49,000 --> 00:03:52,000
So we'll say num1, see you want to increment the value
116
00:03:52,000 --> 00:03:53,000
of num1 by two, right?
117
00:03:53,000 --> 00:03:55,000
You want to add itself by two.
118
00:03:55,000 --> 00:03:59,000
So you can say, I want to add myself, but how much value?
119
00:03:59,000 --> 00:04:01,000
So I have to say equal to two.
120
00:04:01,000 --> 00:04:04,000
So what we're saying is we want to increment num1
121
00:04:04,000 --> 00:04:06,000
by two, even this should work.
122
00:04:06,000 --> 00:04:07,000
Let's try, oh, we got an error
123
00:04:07,000 --> 00:04:10,000
because I forgot the semicolon.
124
00:04:10,000 --> 00:04:12,000
And let me just clear this, run.
125
00:04:12,000 --> 00:04:13,000
And you can see we got nine.
126
00:04:13,000 --> 00:04:14,000
So this is working, cool.
127
00:04:14,000 --> 00:04:16,000
Now it's not just variation, we can do that
128
00:04:16,000 --> 00:04:18,000
for any other operation.
129
00:04:18,000 --> 00:04:21,000
Maybe you want to subtract a number by two.
130
00:04:21,000 --> 00:04:23,000
So we can say subtract equal to two.
131
00:04:23,000 --> 00:04:24,000
So it will reduce the value by two.
132
00:04:24,000 --> 00:04:27,000
You can see got we got five, we can multiply
133
00:04:27,000 --> 00:04:28,000
with some other number.
134
00:04:28,000 --> 00:04:31,000
Maybe I want to multiple the same number
135
00:04:31,000 --> 00:04:33,000
with another number and assign it.
136
00:04:33,000 --> 00:04:36,000
So we've got 567 into 856.
137
00:04:36,000 --> 00:04:37,000
We can divide as well.
138
00:04:37,000 --> 00:04:38,000
And so you can use slash
139
00:04:38,000 --> 00:04:39,000
and all the operators, which you have.
140
00:04:39,000 --> 00:04:41,000
Awesome, so this is shorthand.
141
00:04:41,000 --> 00:04:43,000
Now going back to the additional operator,
142
00:04:43,000 --> 00:04:45,000
and then let's say if you want to add it by two,
143
00:04:45,000 --> 00:04:47,000
we can simply mention the value two, right?
144
00:04:47,000 --> 00:04:49,000
We can also add it by one.
145
00:04:49,000 --> 00:04:51,000
So in this case, if you say added by one, the value
146
00:04:51,000 --> 00:04:55,000
of num1 is seven, and then you are adding it by one.
147
00:04:55,000 --> 00:04:57,000
You can see we got eight.
148
00:04:57,000 --> 00:04:59,000
Now in fact, there's a shortcut for this as well.
149
00:04:59,000 --> 00:05:04,000
What you can do is instead of using num plus equal to one,
150
00:05:04,000 --> 00:05:08,000
you can also say num1 plus plus even this is possible,
151
00:05:08,000 --> 00:05:09,000
let's remove the space in between.
152
00:05:09,000 --> 00:05:12,000
So even this is possible, you can say num1 plus plus,
153
00:05:12,000 --> 00:05:14,000
which is incrementing.
154
00:05:14,000 --> 00:05:17,000
So what we are doing here is something called increment.
155
00:05:17,000 --> 00:05:19,000
And if you're on this call, you can say we got eight.
156
00:05:19,000 --> 00:05:22,000
So this operation, let me just make it one.
157
00:05:22,000 --> 00:05:25,000
So all this operations are similar.
158
00:05:25,000 --> 00:05:28,000
Okay, so why I'm saying similar, why not same?
159
00:05:28,000 --> 00:05:31,000
We'll discuss that in some time, but this is similar
160
00:05:31,000 --> 00:05:33,000
and maybe we have done that before as well.
161
00:05:33,000 --> 00:05:35,000
When we talked about characters, you know,
162
00:05:35,000 --> 00:05:37,000
we remember C plus plus, we done that.
163
00:05:37,000 --> 00:05:40,000
So yeah, we are incrementing here as well.
164
00:05:40,000 --> 00:05:43,000
Now, not just incrementing, we can do decrement as well.
165
00:05:43,000 --> 00:05:45,000
So instead of saying numb plus plus,
166
00:05:45,000 --> 00:05:47,000
we can also say num minus, minus.
167
00:05:47,000 --> 00:05:49,000
So what we are doing now is decrement.
168
00:05:49,000 --> 00:05:52,000
So when you say plus, plus, that is increment.
169
00:05:52,000 --> 00:05:53,000
Let me comment this part.
170
00:05:53,000 --> 00:05:56,000
And here I will say num minus minus.
171
00:05:56,000 --> 00:05:57,000
Now why you have
172
00:05:57,000 --> 00:05:59,000
to commend this, but I want to run this.
173
00:05:59,000 --> 00:06:00,000
So we can say now minus, minus,
174
00:06:00,000 --> 00:06:03,000
let's run this code if, see if this works.
175
00:06:03,000 --> 00:06:04,000
And you can see we got six.
176
00:06:04,000 --> 00:06:07,000
So that's how you can perform this addition,
177
00:06:07,000 --> 00:06:09,000
subtraction, multiplication, division,
178
00:06:09,000 --> 00:06:11,000
and increment, decrement.
179
00:06:11,000 --> 00:06:13,000
In fact, we have also talked about modulars, which is
180
00:06:13,000 --> 00:06:15,000
for finding the remainder.
181
00:06:15,000 --> 00:06:17,000
Okay, now that was about increment decrement, right?
182
00:06:17,000 --> 00:06:19,000
We have talked about that.
183
00:06:19,000 --> 00:06:22,000
Now, instead of using plus plus we can actually use
184
00:06:22,000 --> 00:06:24,000
something called, let me just write it in comment.
185
00:06:24,000 --> 00:06:26,000
So instead of saying num1 plus plus,
186
00:06:26,000 --> 00:06:29,000
we can also say plus plus num1.
187
00:06:29,000 --> 00:06:30,000
You know, just to simplify this example, instead
188
00:06:30,000 --> 00:06:33,000
of saying num1 everywhere, let's say num, so
189
00:06:33,000 --> 00:06:35,000
that maybe you will not get, you will get confused by
190
00:06:35,000 --> 00:06:38,000
that one extra symbol there, which is one,
191
00:06:39,000 --> 00:06:41,000
just ignore all this operation, just focus on this part,
192
00:06:41,000 --> 00:06:42,000
which is num and num.
193
00:06:42,000 --> 00:06:45,000
Also, remove this, also remove this.
194
00:06:45,000 --> 00:06:46,000
Let's see if this is working.
195
00:06:46,000 --> 00:06:47,000
Okay, this is working.
196
00:06:47,000 --> 00:06:48,000
So basically we can also say instead
197
00:06:48,000 --> 00:06:52,000
of saying num plus plus, we can also say plus, plus num.
198
00:06:52,000 --> 00:06:54,000
Now what's the difference between this two?
199
00:06:54,000 --> 00:06:54,000
Even this also work.
200
00:06:54,000 --> 00:06:56,000
Let me just try that and show you.
201
00:06:56,000 --> 00:06:58,000
You can say we got eight, we are incrementing the value.
202
00:06:58,000 --> 00:07:00,000
Even this will give you eight.
203
00:07:00,000 --> 00:07:01,000
But what's the difference?
204
00:07:01,000 --> 00:07:04,000
What's the difference between writing this afterwards
205
00:07:04,000 --> 00:07:06,000
and writing before the variable?
206
00:07:06,000 --> 00:07:09,000
So basically we have a different names to it.
207
00:07:09,000 --> 00:07:11,000
So whenever you perform operation
208
00:07:11,000 --> 00:07:13,000
after the variable, maybe it is for additional
209
00:07:13,000 --> 00:07:17,000
or supply, it is for increment or decrement.
210
00:07:17,000 --> 00:07:21,000
So when you do it post, we say it is post increment.
211
00:07:21,000 --> 00:07:22,000
And when you do pre
212
00:07:22,000 --> 00:07:25,000
before the variable, it is called pre increment.
213
00:07:25,000 --> 00:07:27,000
Okay, so when we are doing it afterwards,
214
00:07:27,000 --> 00:07:28,000
we say post increment.
215
00:07:28,000 --> 00:07:31,000
When you say before it is doing pre increment,
216
00:07:31,000 --> 00:07:32,000
now you might be thinking, hey,
217
00:07:32,000 --> 00:07:33,000
ultimately the value is same, right?
218
00:07:33,000 --> 00:07:36,000
So even if you pull, even if you do this
219
00:07:36,000 --> 00:07:38,000
or this, both will increment the value.
220
00:07:38,000 --> 00:07:41,000
Then why we have two different syntax for it.
221
00:07:41,000 --> 00:07:43,000
Why not just simply one?
222
00:07:43,000 --> 00:07:46,000
The answer is it behaves differently when you try
223
00:07:46,000 --> 00:07:48,000
to assign a value, when you try to fetch the value,
224
00:07:48,000 --> 00:07:52,000
let me repeat, these two operations will behave differently
225
00:07:52,000 --> 00:07:54,000
when you try to fetch the value.
226
00:07:54,000 --> 00:07:56,000
Okay, let me tell you what I'm talking about.
227
00:07:56,000 --> 00:07:57,000
Let me show you it here.
228
00:07:57,000 --> 00:07:57,000
Let's come back here
229
00:07:57,000 --> 00:08:00,000
and let's say I have a different variable, which is int,
230
00:08:00,000 --> 00:08:02,000
and then I'm storing that in result
231
00:08:02,000 --> 00:08:04,000
and what I'm storing is num
232
00:08:04,000 --> 00:08:06,000
or maybe I will say plus, plus num.
233
00:08:06,000 --> 00:08:09,000
Okay, now think about this and maybe you can pause the video
234
00:08:09,000 --> 00:08:11,000
or whatever you wanna do.
235
00:08:11,000 --> 00:08:14,000
Just think about what will be the value of result.
236
00:08:14,000 --> 00:08:16,000
I will say result here to print,
237
00:08:16,000 --> 00:08:18,000
and maybe you have done your thinking, let's run this
238
00:08:18,000 --> 00:08:20,000
and you can see we got eight.
239
00:08:20,000 --> 00:08:22,000
So what is happening here is the initial value
240
00:08:22,000 --> 00:08:23,000
of num is seven.
241
00:08:23,000 --> 00:08:26,000
And then you are saying plus, plus plus num.
242
00:08:26,000 --> 00:08:28,000
That means you are incrementing it, that
243
00:08:28,000 --> 00:08:31,000
incremented value will be assigned to result.
244
00:08:32,000 --> 00:08:34,000
And then you got result, which is eight.
245
00:08:34,000 --> 00:08:35,000
So initially I've also mentioned
246
00:08:35,000 --> 00:08:37,000
that this two will be incrementing.
247
00:08:37,000 --> 00:08:39,000
So we can also say num plus plus, even they should
248
00:08:39,000 --> 00:08:40,000
be doing the same thing, right?
249
00:08:40,000 --> 00:08:43,000
Let's try, oh, there's a twist here.
250
00:08:43,000 --> 00:08:46,000
So plus plus num and num plus plus are not same.
251
00:08:46,000 --> 00:08:48,000
I mean it works same with their individual statement,
252
00:08:48,000 --> 00:08:51,000
but when you try to fetch the value from them when they're
253
00:08:51,000 --> 00:08:53,000
performing the operation, they behave differently.
254
00:08:53,000 --> 00:08:56,000
So let's understand why it is called pre increment
255
00:08:56,000 --> 00:08:58,000
and what is called post increment.
256
00:08:58,000 --> 00:09:00,000
So whenever we use assignment operator with this operation,
257
00:09:00,000 --> 00:09:03,000
what it will do is it will check, hey,
258
00:09:03,000 --> 00:09:04,000
we have an operation which is num plus plus,
259
00:09:04,000 --> 00:09:06,000
but that's a post increment.
260
00:09:06,000 --> 00:09:09,000
So first it will fetch the value of num, let me repeat.
261
00:09:09,000 --> 00:09:12,000
It will first fetch the value
262
00:09:12,000 --> 00:09:15,000
and then increment.
263
00:09:15,000 --> 00:09:16,000
In fact, it looks like one statement,
264
00:09:16,000 --> 00:09:18,000
but we have two statements here, right?
265
00:09:18,000 --> 00:09:20,000
In fact, we have this statement here,
266
00:09:20,000 --> 00:09:22,000
num one equal to num plus one.
267
00:09:22,000 --> 00:09:26,000
So what it'll do is it'll first fetch the value of num,
268
00:09:26,000 --> 00:09:27,000
which is seven in this case,
269
00:09:27,000 --> 00:09:30,000
and then it will increment the value of num
270
00:09:30,000 --> 00:09:32,000
and will make it eight, okay?
271
00:09:32,000 --> 00:09:34,000
But then what is assigned to result is seven itself,
272
00:09:34,000 --> 00:09:37,000
and then you are printing that seven here, okay?
273
00:09:37,000 --> 00:09:38,000
But what will happen when you
274
00:09:38,000 --> 00:09:40,000
say, let me just write it here.
275
00:09:40,000 --> 00:09:44,000
So we'll say end result is equal to plus, plus num.
276
00:09:44,000 --> 00:09:47,000
In this case, what will happen is it will first increment
277
00:09:47,000 --> 00:09:49,000
and then fetch the value, okay?
278
00:09:49,000 --> 00:09:51,000
So it will first increment itself
279
00:09:51,000 --> 00:09:52,000
and then it will fetch the value.
280
00:09:52,000 --> 00:09:54,000
And that is what is going in result.
281
00:09:54,000 --> 00:09:55,000
I hope this makes sense, right?
282
00:09:55,000 --> 00:09:58,000
So this is how your code works when you say plus, plus num
283
00:09:58,000 --> 00:10:01,000
and num plus plus, in fact, we'll do some more operations
284
00:10:01,000 --> 00:10:03,000
with this once we get started
285
00:10:03,000 --> 00:10:06,000
and it will make much more sense later.
286
00:10:06,000 --> 00:10:08,000
So in the operators, we have talked about other
287
00:10:08,000 --> 00:10:09,000
thematic operator until this point.
288
00:10:09,000 --> 00:10:10,000
So whatever option you have done, it is
289
00:10:10,000 --> 00:10:12,000
for the Arithmetic purpose, right?
290
00:10:12,000 --> 00:10:14,000
Addition, subtraction, multiplication, and division.
291
00:10:14,000 --> 00:10:16,000
But what if you want to compare to values?
292
00:10:16,000 --> 00:10:18,000
What if you want to have compound conditions?
293
00:10:18,000 --> 00:10:20,000
Okay, what are those things, we'll talk about
294
00:10:20,000 --> 00:10:22,000
that in the upcoming videos.