Welcome to primary school mathematics again. Back then, we
learnt about addition, subtraction, multiplication and division. And these
operators are at the core of Java and many other programming languages.
So why do we need operators? Assume we have declared two
integers int j = 8; and int k = 9; Without performing some sort
of operation on the integers, they are useless. They don’t tell us much.
However, we can add, subtract, multiply or divide the two integers. We can even
perform a complex calculation that involves more than one operator on the two
integers.
Mathematical
Operator
|
Equivalent
Java Operator
|
+
|
+
|
-
|
-
|
x
|
*
|
÷
|
/
|
Table 1: Operators used in mathematics and their Java
equivalents.
Now let’s say we want to operations on two values in Java.
For short integers: short m = 2; and short n = 7;
Addition: m + n;
Subtraction: m – n;
Multiplication: m * n;
Division: m / n;
For integers: int i = 5; and int j = 6;
Addition: i + j;
Subtraction: i – j;
Multiplication: i * j;
Division: i / j;
For long integers: long p = 26; and long q = 10;
Addition: p + q;
Subtraction: p – q;
Multiplication: p * q;
Division: p / q;
For floats: float f = 15.5; and float g = 12.6;
Addition: f + g;
Subtraction: f – g;
Multiplication: f * g;
Division: f / g;
For doubles: double d = 14.8; and double s = 12.7;
Addition: d + s;
Subtraction: d – s;
Multiplication: d * s;
Division: d / s;
You remember in elementary school when we were taught about
operator precedence? You don’t? Okay. Let me remind you. Say you want to
compute 5 + 6 – 3 x 8 / 4. Some operators are given precedence over others. My
teacher told me that whenever I have such a problem, I should use the mnemonic
BODMAS, which stands for (Brackets Of Division Multiplication Addition and
Subtraction). So I would first calculate values in brackets, then do the
divisions, then the multiplications, then the addition and finally the
subtraction.
There you have it. I’ve just saved you the trouble of looking
for your elementary math books :-)
Java also has its own operator precedence rules, such that it
will prioritize some operators over others. Understanding and maybe even
memorizing these operators can help you make fairly efficient programs.
However, if you cannot memorize the operator precedence table, don’t worry,
because I also haven’t. A way around this is to use brackets to separate the
calculations that you’d like to be done separately.
For example, say I have i x j / k – m + n;
If I would like k – m + n to be computed alone and i x j to
be computed alone, I can surround the two fragments with brackets like this: (i
x j) / (k – m + n); This ensures that before the division takes place, i x j is
computed and k – m + n is computed.
Boolean Operators
In the last tutorial, we saw that boolean datatypes are
either true or false. So, in Java, you can type a statement that Java will
check and tell you whether it is true or false. How is this important? Imagine
that you want to switch on your TV. What will you do? You will check whether
the socket is on. If it is on, you will switch on the TV. If the switch is off,
you will have to switch it on before switching on the TV.
So, you will reason like this:
Scenario 1: Is the switch on? Yes…. Then switch on the TV.
Scenario 2: Is the switch off? No…. Then switch on the
socket…. Is the switch now on? Yes…. Then switch on the TV.
Operator
|
Meaning
|
==
|
Is equal to
|
!=
|
Is not equal
to
|
<
|
Is less than
|
>
|
Is greater
than
|
<=
|
Is less or
equal to
|
>=
|
Is more or
equal to
|
!
|
NOT
|
Table 2: Boolean operators and their meanings in Java.
In Java, we normally put boolean operators between the items
we are comparing. For example:
1 == 1; is true
1>=5; if false
1<8; is true
However, we normally place the ! operator before the
statement in question. For example, we can state that boolean isChecked = true;
If we want to change it to false, we can write !isChecked.
This will change isChecked from true to false.
Figure 1: Java code showing operations on integers. Answers
are displayed on the right.
Figure 2: Java code showing operations on float and double.
Answers are displayed on the right.
Notice now that by using floats and doubles, we get answers with decimals.
Figure 3: Java code showing that you can get different
answers by using brackets. Answers are displayed on the right.
Figure 4: Java code showing boolean operators. Answers are
displayed on the right.
Notice that I have first declared boolean isChecked = false;
But in the last statement, I have written !isChecked and what this does is give
isChecked a value opposite to what it had. This is why the last statement
prints out true instead of false on the console.
Play around with different datatypes and different operators
in Java. You’ll love it. And that’s how you learn…by actually doing.
There are other operators known as binary operators, but I’ll
cover those in another tutorial.
Now we know how to declare datatypes, give them values and
perform calculations on them. Cool!
No comments:
Post a Comment