Having variables and values in our program
doesn't really help us if we can't modify those values in some way.
We need to be able to perform math operations like addition and multiplication.
You want to change our variables using tools other than just
assignment. In this lesson I'm going to show you how you can use Java operators in your
programs.
There's only two goals for this lesson.
One, know what operators are, and two
know how to use them. This should be a fairly simple lesson.
We'll cover all the operators for the most part. Operators are just the symbols
you're used to using in arithmetic There's a few exceptions and we'll cover that.
We're splitting this into two lessons.
The first video will cover what are operators. We'll also talk about assignment,
arithmetic, and unary operators. Next lesson
we'll finish with the rest the Java operators. So
what are operators? Operators are special symbols that perform operations on one,
two, or three values or variables. For example
this is a plus operator. It takes two values 1
and the 2 and returns the result of addition. So the result of the operation
makes the statement "count = three". The equal sign is also an
operator.
This takes whatever variable is on the left and assigns it to the value on the
right.
We've seen plus and assignment operators many times already in this course,
but there's many other operations you can use. Here's the complete list of
operator categories you can use in Java.
These operators all return a single result.
Let's look at arithmetic, assignment, and unary operators first.
The assignment operator is probably looking pretty familiar by now.
The important take way here is the assignment is
one single equal sign. The value returned is the variable set
to whatever's on the right hand side. The right hand side can be literals,
variables, or even other operations. The rule is
whatever type you define for your variable on the left
must also be the same type as what is on the right.
If it's a number primitive, you must be able to do a widening
conversion. Otherwise you must cast the right value to the left value.
We saw a bit of casting in the variables lesson.
Let's go look at code real quick to make this assignment clear. So we can define
a variable called 'a' and set 'a' equal to one. That's the simplest assignment.
The variable of type on the left gets set to the value on the right.
If it's a reference type, you're getting the reference assigned,
not the value. Operations can be chained. They have different
rules on direction, but for the most part it is what you'd expect.
Equality is always left gets a value on the right
and it has to make sense too. You can't set
"a = 2 = 3". You can set
"a = b = 3".
Coding like this is really discouraged though. You really should
assign
each variable in their own Java statement. So I'm just showing you it can be done and
this is how it works.
So working from the right in this case 3 get set to 'b'
and 'a' gets set to 'b'. The last thing the note is the equals operation
makes the expression a Java statement. Arithmetic operators are the ones you're most
familiar with.
We'll call them the map operators. The first four should be
very familiar to you. It's your basic operators
addition, subtraction, multiplication,
and division. The last one is called modulus.
When you do division with integers, you can have a remainder.
So 5 goes into 6 one time with the remainder
of 1. 5 would go into 5 one time, with the remainder 0.
Let's go back to the code and check this out. We use the assignment operator with these.
The reason is math operators aren't enough to make Java statements.
You can make a value with math operators, but Java doesn't know what to do with it
until you assign it to something. So again we use
'a' and set it equal to 1 + 2. So
'a=3'. Let's control the order of operations again.
Let's do 4 div 2 + 3.
When we run this, we get five. So the operations are moving from left to right.
We do 4 div 2 and get 2.
Then we add three, and that's equal to 5. Let's put parentheses around the two
plus three.
That means we'll calculate 2 + 3,
then divide four by the result. We should get
the answers 0.8. We run it and
we get zero. That's not what we expected.
What happened here? So here's a hint.
Look at the type of 'a'. It's an integer. Everything here is an integer.
So the div is an integer division.
5 goes into 4, 0 times with the remainder 4.
The remainder is lost. So
types do matter. Let's switch div to modulus and run it again.
We get three and four.
In the first case we have 4 mod 2 which is zero.
No remainder. Then we add 3 and we get 3.
In the second case, we add 2 and 3 to get 5.
5 goes into 4 zero times with the remainder
4. So that's how modulus works. One other thing to note is when the modulus of a
number equals 0, that means the number is a factor
the other number. So here since the modulus is zero,
5, 10, 15, and 20 and so on are factors of 5.
That's an important thing to remember. It's been a popular interview question
of late
and we're going to use it again when we cover loops. The unary operators
perform operations on a single value
and return a single value. The first one is just sign.
This makes a value or variable
positive or negative. You'll definitely want to use parentheses with these,
otherwise it can get tricky knowing if you meant addition and subtraction
or changing the sign. The other four unary operators
are prefix and postfix addition and subtraction.
So if we need to add one to a variable
we can just use plus plus. The plus plus is the same as saying
i + 1 and assigning it to i. Minus minus is subtraction.
You'll often see these used as part of a more complex job a statement.
So why do we repeat the symbols on a different sides of the value?
Lets play with each one of these and see what happens. Let's set i = 1.
Then lets set j equal to 4 plus
(++ i). And the next line we'll do it again but
do j = 4 plus
i++. What do you think the value difference will be here?
On the first case we get 6.
What's happening is we're getting i, adding one to it,
and then returning 2. 2 + 4
equals 6. In the next case, here we get
i, add 4 to get 5,
and then 1 to i. In both cases
i ends up equal to 2. The urinary operator
determines if i gets incremented before or after it's used in the arithmetic.
If it's on the left its before, if it's on the right
it's after. Let's do the minus minus operator for completeness.
I'm gonna change the pluses to minuses like this. Now what do you think we're going to get?
Well we get 4 and 5.
So the first one we took i equal one
subtracted it which left 0, and then added
0 to 4 leaving 4. The second one
we took i is one, added to 4
giving five, and subtracting one
from i leaving 0. So in both cases
I ends up equal to 0. Okay hopefully that made sense.
If not I encourage you to play with the operators and code on your own.
You should also try the exercises if you have any questions.
DeegeU.com.
Hey! Thanks for watching the video.
There's a quick quiz for this on DeegeU.com to gauge how much you
learned.
If you like the videos you are seeing, please let me know by liking the video and
hitting the subscribe button for the
DeegeU channel on YouTube. I'd really appreciate that. If you have concerns or
questions, please leave them in the comments below
or on DeegeU.com. There's a poll on the front page of
DeegeU.com so you can let me know what topic is covered
next. Thanks for watching, and see you in the next video.