| Operator | Meaning | Order of Operation |
|---|---|---|
| - | unary minus | first |
| + | unary plus | first |
| * | multiplication | second |
| / | division | second |
| % | remainder | second |
| + | addition | last |
| - | subtraction | last |
All of these operators can be used on floating point numbers and on integers.
An integer operation is always done with 32
bits or more. If one or both operands are 64 bits (data type
long) then the operation is done with 64 bits.
Otherwise the operation is done with 32 bits, even if both
operands are smaller.
![]() |
|
Remainder
% will calculate the remainder for a division. For example, 17%5 will evaluate to 2 It is limited to use for integer division. You cannot use it with floating point division.
When doing integer division, by careful use of / and % you can calculate exact answers when dividing.
The remainder operator can be used with negative integers. The rule is:
-
Perform the operation as if both operands were positive.
-
If the left operand is negative, then make the result negative.
-
If the left operand is positive, then make the result positive.
-
Ignore the sign of the right operand in all cases.
Examples
Here is a sample program for you to experiment with:
//Java program to experiment with remainder operator //Created by M Meijers March 2005 class Remainder
{
public static void main ( String[] args )
{
int quotient, remainder;
quotient = 17 / 3;
remainder = 17 % 3;
System.out.println("The quotient : 17/3 = " + quotient );
System.out.println("The remainder: 17%3 = " + remainder );
}
}
|
The following program will show you the difference between using integer vs floating point division:
//Java program to highlight integer vs floating division
//Created by M Meijers March 2005
class Arithmetic
{
public static void main ( String[] args )
{
int int1=3, int2=4; //declare and initialise 2 integer variables
float float1=3, float2=4; //declare and initialise 2 floating variables
int intAnswer; //declare integer answer
float floatAnswer; //declare floating answer
intAnswer=int1/int2; //calculate integer division answer
floatAnswer = float1/float2; //calculate floating division answer
System.out.println("Integer 3/4= " + intAnswer );
System.out.println("Floating 3/4 =" + floatAnswer );
}
}
|
![]() |
|



