We can use math operators to operate on values.
So far, you learned about five types of values into four whole numbers, long for large numbers, double
for decimals, car for single characters and string for a string of characters also known as text.
You can use math operators to play with these values and operate on them.
And so in the next two videos, we're going to look at all the math operators in Java.
Namely, the plus operator, which is used to add values, the minus operator to subtract values, the
multiplication operator represented by the asterisk, the division operator represented by the front
slash the modulus operator, which returns the remainder of a division, the add one operator, which
increases their value by one B. Subtract one operator, which decreases a value by one.
The add by operator, which increases the value by the number on the right, and you subtract by operator,
which decreases the value by the number on the rights.
What a list.
All right.
For this lesson, we're going to start by looking at the first four operators.
First thing I'll need you to do is create a new class by yourself inside the section to folder, making
you file name math operations Java and make sure the class has a main method.
OK, first, we need to make some variables because math operators like you operate on values, so we'll
need to set up some variables with store values inside them.
We'll start by making two into variables and bag of sweets one.
Is equal to five.
And int.
Bag of sweets to.
Is equal to 10.
Now we're going to make two long variables.
Long stars in Milky Way.
Is equal to and there are 2.5 billion stars, civil right to five and eight zeroes, one, two, three,
four, five, six, seven, eight.
And don't forget the L to reassure Jova that, hey, we are going to store this in a long variable and
that will say long.
Stars in.
Andromeda.
Is equal to and there are a trillion stars, so we'll say one and 12 zeroes, but don't forget the al.
Finally will make two double variables, double test score is equal to six point seven.
And double bonus marks.
Is equal to 2.5 in any case, we created six variables to enter variables.
Too long variables and two double variables.
Now we can start using operators and as we play around with these operators, you're going to notice
a very interesting pattern in operation between home numbers.
Always returns a whole number and an operation between decimals will always preserve the decimal.
I want you to keep this in mind as we go through every operator.
We'll start with the plus operator, you've seen it before, the plus operator adds to values and the
symbol is without a doubt, the plus side.
So we're going to add every pair of end long and double values and print each results system.
Dot out, dot print line.
Bag of sweets, one plus bag of sweets, two.
Once again, systemd out front line stars in Milky Way, plus stars in Andromeda.
Third time system got out front line test score, plus bonus marks.
We're in a compiler code.
Now, I ask you, isn't it nice when things are consistent, because notice that adding two whole numbers
returns a whole number, you saw this from adding the Enten long values and just the same adding to
decimals gives a decimal.
You saw this for the double value.
It's always nice to see a pattern because it makes coding easier when you can predict what type of return
value, what type of result you're going to get.
But don't forget, the plus symbol is special because it goes beyond just numbers.
We can use it to blend values into a string.
So we'll start with the bag of sweets now before you do what I'm about to do.
Just watch me do it first.
I'll give you time to pause the video in a bit.
But what I'm going to do is go to the resources article, math operators, resources, and I'm going
to copy every sentence from part one.
Pace it at the very bottom.
Then, while everything is highlighted.
Press command slashed the comment at all, if you're using a Mac or control slash for windows, and
this is probably a good place to pause the video and do what I just did.
All right, by the way, don't mind the front slashes their only comments and have zero effect on your
code.
Besides, if you did the workbooks and I encourage you not to skip any of them, you would have already
encountered comments in any case.
I'm going to cut and paste the first sentence into a print statement.
When I put string quotes around it.
And now look for the placeholder, disconnect the string on both ends.
And you're going to pace the operation on to the placeholder.
Put it in brackets, that's really important.
Then use the plus symbol to blend the result of the math operation into the strike call.
Compile the code and run it.
This looks good, plus embeds the number inside the string.
Now I'm going to minimize the file explorer so that you can see everything I'm doing and notice that
when blending a math operation into a string, I put it inside brackets.
I'll tell you why in just a second.
For now, let's do the same thing for the long and double values.
Copy each sentence into a string, quote.
Substitute the operation for the placeholder.
And then blend that result into the drink.
And I'm just going to quietly do the same thing for the last one.
All right.
Hope that wasn't too hard on compile the code, runit, run it.
This looks pretty good.
All right, now let's try removing the brackets, do it for the first one.
Now recompile the code and run it.
I'm pretty sure that five plus 10 isn't 500 in ten.
That's because Java didn't actually add the numbers up, it just connected them.
If you don't put brackets, things are going to happen from left to right, the string connects to the
number five.
So now you're sentences.
Fred and George collected five.
And now this new updated sentence connects to the number 10.
There are new sentences, Fred and George collected five, ten, and then we connect the last string,
which results in the sentence being Fred and George collected five, 10 suites.
But when you put brackets, it tells you that this is a math operation, make sure to add these two
numbers first, then whatever the final result is, can that that into the string?
Long story short, just to remember to put operations in brackets if you want to display the result
into a string, that's all.
All right.
Recompile the code and run it and you get the result that we expect.
Go ahead and delete these three comments now.
All right, let's talk about the minus operator, this one is used to subtract two numbers and the code
for subtracting stuff is the hyphen.
So back in our code, we're going to attract every pair of end, long and double values system out print
line.
Bag of sweets to mine, his bag of sweets one.
System systemd out print line.
Stars in Andromeda, minus stars in the Milky Way.
And system dot our print line.
Minus bonus marks.
OK, let's compile the code.
And the pattern stays intact, subtracting whole numbers returns a whole number, the supplies to the
end in long values and subtracting decimals returns a decimal.
Once again, we can join every member into a string, you're going to copy the sentences down below
and we're going to copy each one into string quotes.
One by one.
Great.
We're done with that now we're going to disconnect e string where there is a placeholder.
So here.
Once you do that, substitute each placeholder at the operation whose return value we want to blend
into the string.
And notice that I'm putting each math operation inside brackets, if you don't do that, you'll get
an error.
So, yeah, like I said before, if you're trying to blend the result of an operation into a string,
whatever the operation is, put it in brackets and you should be good.
All right, now recompile the code and run it.
Looks good.
The next operative we can talk about is the multiplication operator, which we use to multiply numbers
and encode the symbol to multiply numbers as the asterisk.
So we're going to multiply the intense and double values system, dot out, dot print line.
Bag of sweets, one times, bag of sweets to.
We'll do the same thing here.
Front line test score.
Times bonus marks.
We're in a compiler code.
Then run it.
And the pattern is strong because once again, multiplying whole numbers returns a whole number and
multiplying decimals preserves the decimal.
And once again, trying to connect each result into a string, so we'll copy these sentences into street
books.
One by one.
And we're going to substitute each placeholder with the results for multiplying each pair of numbers.
All right, that is that you can go out and compile the code and run its.
And our storyline is really developing.
You're likely wondering why and multiply the long values, and that's because multiplying two billion,
500 million by a one trillion, as I calculate it, equals to sextillion 500 quintillion.
That's a lot of illions.
And the range for long variables is nine quintillion, so even long can't store that big of a number,
and if you were to try to perform this multiplication, it wouldn't work because Java is just going
to return a random number.
In any case, the last operation we're going to talk about is the division operator, and it's represented
by the front.
And we can use it to divide numbers.
We're going to start with the end and long values system output line, bag of sweets, too, divided
by a bag of sweets one.
And once again, system, dot, dot, print line.
Stars in Andromeda, divided by the stars in the Milky Way.
Compile this code.
And run it and as I expected, dividing whole numbers returns a whole number.
We see this through the end and long values.
All right, now we'll divide the double values.
System not out front line test score divided by bonus marks.
And like always, double preserves the decimal places, no matter what operation you're using.
All right, well, unless they going to do before we wrap up this video's to connect each number to
a string.
So, as always, copy each sentence into a string, quote.
One by one.
Take your time.
Once you do delete the comments and now we're going to replace each placeholder with the result of each
division.
All right, we can go back and compile the code.
And runit.
Great stuff.
We created quite the storyline, and if you feel like things were getting repetitive, this means you're
getting used to it, which is very good.
And so in this lesson, we used four of the nine math operators, you also saw a very nice pattern.
A math operation between whole numbers will always return a whole number and a math operation between
decimals will always preserve the decimal.
And it's nice to see this pattern because it makes coding easier when you understand how things are
going to behave.
We saw this pattern when we applied the first four operators and now I'm going to show you how to use
the remaining five.