Hello there 🙂 from this lesson we are going to discuss about various types of operators used in Java. You already know most of these operators because they are in your elementary school mathbook. Let’s see.
They are easy to understand! But DON’T skip this lesson as well! You have to have a very clear understanding about them.
Remember the binary operators you have learnt in school? Addition +, subtraction –, division /, multiplication x things 🙂
Binary operators always operate between two numbers only.
Eg: 3+2 so two numbers. What about 3+2+5 there also what we actually did was 3+2=5 first and then 5+5. Isn’t it??
Alright. So now we have the same operators in Java as well.
They are used to do operations between two numerical types such as byte, int, long, double….
Followings are the symbols used in Java;
|Addition||+||23+4.5, x+1, x+y|
|Subtraction||-||2-3, x-53, 34-y-x|
|Multiplication||*||2*y, x*2*y, x*x, 2*x+y|
|Division||/||3/2.0, 10/2, x/y|
The modulus operator of x%y gives the remainder when x is divided by y. So, 30%7 is 2 and 50%2 is 0.
There is one important thing you should remember. When two ints are divided like 7 / 2, then, unfortunately you won’t get 3.5 as the answer. You will get 3 just by omitting the decimal points.
This is because integers cannot have a decimal part. So,
if you want to have the correct answer, you have to make one of the two numbers in to decimal number. Just REMEMBER that!!
Eg: 7 / 2.0 or 7.0 / 2 or 7.0 / 2.0
These operators have a precedence rule. The ones with higher precedence will operate first.
*, / and % have the same precedence.
+ and – have the same precedence. But they have a lower precedence than the other three.
The ones with the same precedence will operate from left to right.
Eg: 3 + 20 / 5 * 3 – 8 % 3
The answer is 13.
What happens here? Let’s see the operators with higher precedence. / → * → %. Though + is occurred first, it won’t start first.
The one which operate first is /. So 20/5 = 4.
Then this 4 is multiplied by 3. 4*3 = 12.
Now the expression is something like this.
3 + 12 – 8 % 3
The next – operator is skipped and 8 % 3 is operated. 8 % 3 is 2. So now the expression is,
3 + 12 – 2
Finally, the + and – operators are going to work from left to right.
3 + 12 = 15 and 15 – 2 = 13.
The relational operators will compare two entities according to a condition and gives either true or false according to the condition. That is, the output is a boolean value.
For example, x >= y checks whether x is larger than or equal to y and gives either true or false using the values of x and y.
If x = 10 and y = 8 then, x >= y means 10 >= 8. It’s true. So the output is the boolean value true.
Other than this >= condition, there are many other types of conditions. OK. we’ll see.
Let’s take two variables x and y.
- x>y means x is larger than y
- x >= y means x equals or larger than y
- x<y means x is less than y
- x<=y means x is less than or equals y
- x!=y means x is not equal to y
- x==y means x equals y
** NOTE : See, instead of single = mark, we have used == that is, two equals. Really why this is done because the single = mark is for the assigning statement in variables.
Eg: int x = 10; → assigning 10 to x.
But x == 10 → used to check whether x equals 10. If it is not 10, false is given out. If x is 10, true is given out.
This will be more clear when we do the lesson, if statements.
There are three boolean operators, AND, OR and NOT.
So here is the explanation about them…
|| – OR operator
When there are two relational operator conditions, this operator is used to give the true output either one of them is correct.
(a>120 || a<0) means that “either a is larger than 120 OR less than zero………” Either one of the conditions is true, the output will be true.
&& – AND operator
This operator gives true only if BOTH conditions in the left and right of it are true.
(a>10 && b<20) means, “if a is larger than 10 AND b is less than 20…..” BOTH of those conditions have to be true to output true. If any condition is false, then the whole output is false.
! – NOT operator
NOT is used to change the true state in a condition to false or false into true.
If !(age<18) is given, the way of getting the result is as follows:
If age is less than 18, then true will be given out. The ! operator will inverse this output. So ultimately, if age is less than 18, false will be given out as the net result.
!(age<18) means that “age is NOT less than 18”. This gives the same meaning as “age is larger than 18”. That is, (age>18) Isn’t it?? 🙂
Updating a variable
Increment and decrement by 1
In the next tutorials, you will frequently find this;
if x is a variable,
x = x +1;
x = x-1;
These types of operation are usual in programming. So you have short methods to represent them.
x++; / ++x; is same as x = x+1;
x–; / –x; is same as x = x-1;
Combined assignment operators
These are somewhat similar to the previous operators. Here, the shortcuts for doing arithmetic operations and updating the variables are illustrated.
- x = x + 2; is equal to x += 2;
- x = x – 3; is equal to x -= 2;
- x = x / 50; is equal to x /= 50;
- x = x % y; is equal to x %= y;
- x = x && y; is equal to x &&= 2;
Type Cast Operators
If you want to change the type of some data in a variable, you can use the type cast operator.
Eg: Let x be a variable of type int.
Suppose x has to be a random integer between 1 and 10.
To generate a random decimal number between 0 and 1, there is a method called Math.random(); But it will just generate a number between 0 and 1. Not a whole number!
So in order to make it a number between 0 and 10, you can of course multiply it with 10.
10*Math.random() will now generate a number between 0 and 10.
But, again, note that, this may not be an integer. For example, if Math.random() generates 0.54, then, 10*Math.random() will form 5.4 which is not an integer.
So in order to eliminate this decimal part and make it an integer, use the type cast operator (int).
Now we can assign it to x.
x = (int) (10*Math.random()); will generate a number from 0 – 9. NOT including 10. If you want to include 10 as well, just do this,
x = (int) (10*Math.random()) + 1;
Remember, using the parenthesis before 10 is compulsory.
Because, the type-cast operator has a higher precedence than *. So, if we forgot the brackets, (int) 10 will execute first. That will be 10 itself.
Then 10*Math.random() is again, a decimal number. Wrong Wrong!
Ooh….End of a long lesson 🙂 In the next lesson, we are going to learn how to give basic input when running a program