How to use the Java modulo operator
When division doesn’t work out perfectly, you’re left with a remainder. The Java modulo operator is there to account for this in your code. The operator is significantly more reliable than other solutions and is primarily used to determine whether numbers are even or odd.
What is the Java modulo operator?
In all the common programming languages, you can perform the basic arithmetic operations of addition, subtraction, multiplication and division. You can also use Java operators to display and solve complex calculations. When it comes to division in particular, there is always the risk that there is a remainder. For example, if you divide 11 by 4, you’ll have a remainder of 3 (2 x 4 = 8, 11 - 8 = 3). This can lead to problems in programming.
- 99.9% uptime
- PHP 8.3 with JIT compiler
- SSL, DDoS protection, and backups
That’s what the Java modulo operator is for. It’s also known as the remainder operator, as it determines and returns the remainder after two numbers are divided. There are various situations in which it is extremely important. The operator can be used, for example, to find out whether a number is even or odd or whether a number is prime. While there are also other ways to determine the remainder, they quickly reach their limits. The following example will illustrate that.
What is the remainder operator used for?
Put simply, division involves checking how many times a divisor fits into a dividend. The result is the quotient. If the dividend and divisor are both stored in the primitive Java data type int (or integer), the remainder can easily be expressed without Java modulo. The code would look as follows:
We can then use the Java command System.out.println
to view the result:
This option is a bit unwieldy but is in theory possible. However, we’ll run into problems as soon as at least one of the operands is a float or double, that is a floating number data type. In this case, the same calculation will give us a different result:
What is the syntax of modulo in Java?
The Java modulo operator, on the other hand, will always return the exact remainder. It performs division but only shows the remainder, not the quotient. It’s initialized with a percent sign, meaning its syntax looks as follows:
Java modulo would look as follows when used in the above example:
The output is then:
How to determine whether a number is odd or even
Java modulo can be used to find out whether a number is even or odd. It follows from simple logic: If you divide any number by 2 and the remainder is 0, it’s even. Otherwise it’s odd. We’ll illustrate this with an if-else statement.
As expected, we get the following result:
How to determine the remainder of a negative number
You can also use Java modulo to get the remainder when the dividend or divisor is negative. Here’s a simple example:
Since the dividend is negative, the remainder is negative as well:
But you might not always want to get a negative result. If you want to return a positive remainder, you can modify the code a bit. First check if the remainder is less than 0. If so, add the divisor and you’ll get the positive remainder. Here’s how that code will look:
We’ll get the following output: