Something we commonly must do in
our programs is perform arithmetic operations. Java provides three basic
categories of operators for performing math operations. They're the basic
operators, like add, subtract, multiply, and divide, what are called prefix and
postfix operators, and then what are called compound assignment operators. The
basic math operators are just what you would expect, things like add, subtract,
multiply, and divide. The operators we use for these are, again, things that
are very familiar. If I'm adding, I use a plus sign. And when I do things like
add, it doesn't really matter whether I'm using a floating point or an integer.
So, if I say 1 + 2, the answer is going to be 3. For subtraction, we use the
minus sign. Again, it doesn't really matter whether we're using floating point
or integers. If I subtract 4 from 5, I get 1. If we multiply, we use an
asterisk, 4 * 2 is 8. And all that stuff is straightforward. As we get to the
divide operations, though, there are some differences between floating point
and integer. If I take 13.0 and divide it by 5.0, that's a floating-point
operation. Floating points have fractions, so I will get 2.6. But now if I take
the integer 13 divided by 5, that's an integer operation. Integers don't have
fractions, so I'll get just 2. It doesn't matter what the fractional portion
would be. It's just dropped. There's no rounding or anything. It's just the
whole number of times that you can do the division. So, 5 goes into 13 two
whole times. There's another operator called the modulus operator, also known
as the remainder operator, and that gives you the remainder of the divide. So,
let's look at the integer first on this one. If I say 13 % 5, that's the
percent symbol, my answer will be 3. Five goes into 13 two whole times, leaving
3, 5 * 2 is 10, subtract 10 from 13, you get 3. And you can do a modulus on
floating points as well. So, if I do 13.0 % 5.0, I'll have a remainder of 3.0.
As we look at the prefix and postfix operators, they allow us to operate
directly on a value. The ++ symbol is the increment. It increments a value by
1. The ‑‑
symbol decrements a value by 1. So, if you look here, if I have a variable,
myVal, and I set it to 5, if I say print out ++myVal, that's the increment
operator. Because it's before the actual variable, the ++ is before the
variable, that's a prefix, which means that the operation is applied before we
get the value back. So, the result of this print statement is 6, the 5 was
incremented, so we print out it was 6. If I print myVal again, it's still 6.
But now if I move the operator after the variable, it's now a postfix operator.
And what that means is that I get the value back, and then the operation is
performed. So, if I look at similar code here where I have my variable, myVal,
I set it to 5, but this time when I print it out, I say myVal++, so I'm doing a
postfix increment. The value printed will be 5 because that 5 was returned. But
myVal is now incremented. If I print myVal now, I will get 6. The last category
of math operators is what we call compound assignment operators. And what these
do is they combine an operation and the assignment. So, basically, it looks at
the right side of the operator, takes whatever that is, and applies the
operation to the left side, and then stores the value into the left side. So,
let's see what that looks like. If I go ahead and have myVal, and I set it
equal to 50, if I say myVal ‑= 5, so the right hand side's value is 5, the left hand side value
is currently 50, it applies the operation, which is the minus sign, so it
subtracts 5 from 50, and then stores that value back into myVal. So, if I print
out myVal, myVal has 45. And they're available for all five basic math
operations. In my experience, I use the ‑= and the += far and away the most, but you do find occasions to
use the other ones as well. So looking at another example, if I have an int
result = 100, and I have 2 more variables, val1 = 5 and val2 = 10, if I now say
result /= val1 * val2, remember that it's going to take the complete result of
the right side. So, if I look a val1 * val2, the result of 5 * 10 is 50. It
then takes the existing value in result, which in this case is 100, and then it
then takes that operator, the divide, and performs that operation between them.
So, it's going to divide 100 by 50 and then store that back into our variable
called result. So, we print out the value of 2.
Comments
Post a Comment
We appreciate your valuable Suggestions/Feedback