At this point, we have already discussed the different data types and also we have explained about variables and so as arrays. So let’s now go to the java operators.

## Topics Covered

- order of operators
- different types of operators
- How to use each type of operators

## Operators Precedence

Similarly, in java programming the operators available have some sort of precedence with one another. Below are the list of operators and their precedence is arrange from top to bottom, the top having the highest precedence. And from left to right, with the left having higher precedence.

Operators | Precedence |
---|---|

postfix | expr++ expr– |

unary | ++expr –expr +expr -expr ~ ! |

multiplicative | * / % |

additive | + – |

shift | << >> >>> |

relational | < > <= >= instanceof |

equality | == != |

bitwise AND | & |

bitwise exclusive OR | ^ |

bitwise inclusive OR | |

logical AND | && |

logical OR | |

ternary | ? : |

assignment | = += -= *= /= %= &= ^= |= <<= >>= >>>= |

We will not be discussing all of the above operators but rather we will be tackling only those that is commonly being used.

## Arithmetic Operators

- + , used for addition
- – , used for subtraction
- * , used for multiplication
- / , used for multiplication
- %, used to get the remainder

I will not go into detail the used of the first 4 arithmetic operators since, i believe you are already familiar with it. like for example

int x = 2 + 1; int y = 4 - 2; int f = x/y; float var = f * 1.2f;

From the above example, does it look familiar to you? Yes, it is very very similar to real life mathematics. But in java programming, it’s a little bit tricky because of the data types. As you can see from the above example, particularly on line 3, the result should be 1.5 but since we are dealing with int the result would only be 1. The int data type that has been used automatically truncated the result, removing the decimal digits. If you are interested to get the result of the division of x and y with the decimal digit intact, make the declaration of x,y, and f as type double or float.

Please be very careful in dealing with data type. For example if we have a statement double + int, you should assign the result to type double otherwise, compilation problem will occur. Similarly of you have int/int expression, even if you assign this to type double, it will still not give the proper value. To resolve such predicament, you can either cast one variable to Double/Float, or assign one variable as double/float.

At this point we are done with the 4 mathematical operators, now we can start to tackle the modulo operator %. Basically this operator when used returns the remainder of the mathematical statement. Let’s say we have a statement 3%2, this would evaluate to an integer 1. What do you think happen, why this statement return 1? Basically if we divide 3 and 2, the result would be 3 r 1. The modulo operator get that remainder value which is 1.

So where is the modulo operator being used in the sense of practical usage? One good example is when you have to determine if an integer is odd or even. To give more clarity, let’s define what is an odd or even number. An even number is a number that can be divided into groups of 2. While an odd integer are those numbers that cannot be divided into 2 groups. With this definition, the use of modulo operator is perfect. If it evaluates to 1 for expression number%2 then it is even, otherwise odd.