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

At the end of this tutorial we should be able to fully understand the following:

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

Operators Precedence

I would assume that you are already aware that Mathematical operators follow a certain order of precedence. PEMDAS, sounds familiar right? To give a little background of regarding the order of mathematical operators, lets take one basic example 12/3*2-5. What would be the results? It follows the precedence parenthesis, exponent, multiplication, addition, subtraction in short PEMDAS. If you are interested what would be the result, it is evaluated like this 12/3*2 -5 = 12/6 -5 = 2-5 = -3.

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

Let’s start the discussion with the most basic type which the arithmetic operators. There are 5 arithmetic operators available in java programming, these are the following:

  • + , 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.

The modulo operator is rarely use, so no worry on this but it’s  good to know about it in advance so that when the time comes that you will be needing it, you already have an idea on it’s usage.