- Learning Java by Building Android Games
- John Horton
- 361字
- 2021-07-23 19:02:25
More operators
We can already add (+
), take away (-
), multiply (*
), divide (/
), assign (=
) increment (++
) and decrement (--
) with operators. Let's introduce some more super-useful operators, and then we will go straight on to understand how to use them in Java.
Tip
Don't worry about memorizing every operator below. Take a glance at them and their explanations and then move quickly on to the next section. We will put some operators to use soon and they will become much clearer as we see a few examples of what they allow us to do. They are presented here in a list just to make the variety and scope of operators plain from the start. The list will also be more convenient to refer back to when not intermingled with the discussion about implementation that follows it.
We use operators to create an expression which is either true or false. We wrap that expression in parentheses like this: (expression goes here)
.
- The comparison operator (
==
). This tests for equality and is either true or false. An expression like(10 == 9)
, for example, is false. 10 is obviously not equal to 9. - The logical NOT operator (
!
). The expression(! (2+2 == 5))
tests if something is true because 2 + 2 is NOT 5. - Another comparison operator (
!=
). This tests if something is NOT equal. For example, the expression(10 != 9)
is true. 10 is not equal to 9. - Another comparison operator (
>
). This tests if something is greater than something else. The expression(10 > 9)
is true. - You can probably guess what this does (
<
). This tests for values less than. The expression(10 < 9)
is false. - This operator tests for whether one value is greater than or equal to the other (
>=
). If either is true the result is true. For example, the expression:(10 >= 9)
is true. The expression(10 >= 10)
is also true. - Like the previous operator (
<=
), this one tests for two conditions but this time less than or equal to. The expression(10 <= 9)
is false. The expression(10 <= 10)
is true. - This operator is known as logical AND (
&&
). It tests two or more separate parts of an expression and all parts must be true in order for the result to be true. Logical AND is usually used in conjunction with the other operators to build more complex tests. The expression((10 > 9) && (10 < 11))
is true because both parts are true, so the expression is true. The expression((10 > 9) && (10 < 9))
is false because only one part of the expression is true, and the other is false. - This operator is called logical OR (
||
). It is just like logical AND except that only one, of two or more parts of an expression, need to be true for the expression to be true. Let's look at the last example we used above but switch the&&
for||
. The expression((10 > 9) || (10 < 9))
is now true because one part of the expression is true.
All these operators are virtually useless without a way of properly using them to make real decisions that affect real variables and code. One way we get to use expressions and these decision-making operators is with loops.