Java - Operator Modulus Operator %

What is Modulus Operator?

The modulus operator % has the following form

operand1 % operand2 

The modulus operator is called the remainder operator.

The modulus operator performs a division on the left-hand operand by its right-hand operand and returns the remainder of the division.

7%5 evaluates to 2.

The sign of the result is the same as the sign of the left-hand operand. For example,

float f1; 
double d1; 
f1 = 15.5F % 6.5F; // Assigns 2.5F to f1 
d1 = 5.5 % 15.65;  // Assigns 5.5 to d1 
d1 = 0.0 % 3.78;   // Assigns 0.0 to d1 
d1 = 85.0 % Double.POSITIVE_INFINITY;  // Assigns 85.0 to d1 
d1 = -85.0 % Double.POSITIVE_INFINITY; // Assigns -85.0 to d1 
d1 = 85.0 % Double.NEGATIVE_INFINITY;  // Assigns 85.0 to d1 
d1 = -85.0 % Double.NEGATIVE_INFINITY; // Assigns -85.0 to d1 

Demo

public class Main {
  public static void main(String[] args) {
    float f1; /*from   w w w.j ava2 s .  c o  m*/
    double d1; 
    f1 = 15.5F % 6.5F; // Assigns 2.5F to f1 
    System.out.println(f1);
    d1 = 5.5 % 15.65;  // Assigns 5.5 to d1 
    System.out.println(d1);
    d1 = 0.0 % 3.78;   // Assigns 0.0 to d1 
    System.out.println(d1);
    d1 = 85.0 % Double.POSITIVE_INFINITY;  // Assigns 85.0 to d1 
    System.out.println(d1);
    d1 = -85.0 % Double.POSITIVE_INFINITY; // Assigns -85.0 to d1 
    System.out.println(d1);
    d1 = 85.0 % Double.NEGATIVE_INFINITY;  // Assigns 85.0 to d1
    System.out.println(d1);
    d1 = -85.0 % Double.NEGATIVE_INFINITY; // Assigns -85.0 to d1 
    System.out.println(d1);
  }
}

Result

Special rules for int type modulus operation

If both operands of the modulus operator are integers, the following rules are applied to compute the result.

It is a runtime error if the right-hand operand is zero. For example,

  
int num; 
num = 15 % 0; // A runtime error 
  

If the right-hand operand is not zero, the sign of the result is the same as the sign of the left-hand operand. For example,

int num; 
num = 15 % 6;   // Assigns 3 to num 
num = -15 % 6;  // Assigns -3 to num 
num = 15 % -6;  // Assigns 3 to num 
num = -15 % -6; // Assigns -3 to num because left-hand operand is -15, which is negative 
num = 5 % 7;    // Assigns 5 to num 
num = 0 % 7;    // Assigns 0 to num 

Demo

public class Main {
  public static void main(String[] args) {
    int num; /* w  w w. jav a  2  s. c o m*/
    num = 15 % 6;   // Assigns 3 to num 
    System.out.println(num);
    num = -15 % 6;  // Assigns -3 to num 
    System.out.println(num);
    num = 15 % -6;  // Assigns 3 to num 
    System.out.println(num);
    num = -15 % -6; // Assigns -3 to num because left-hand operand is -15, which is negative 
    System.out.println(num);
    num = 5 % 7;    // Assigns 5 to num 
    System.out.println(num);
    num = 0 % 7;    // Assigns 0 to num 
    System.out.println(num);
  }
}

Result

Special rules for floating type modulus operation

If either operand of the modulus operator is a floating-point number, the following rules are applied to compute the result.

The operation never results in an error even if the right-hand operand is a floating-point zero.

The result is NaN if either operand is NaN. For example,

  
float f1; 
double d1; 
f1 = Float.NaN % 10.5F;     // Assigns Float.NaN to f1 
f1 = 20.0F % Float.NaN;     // Assigns Float.NaN to f1 
f1 = Float.NaN % Float.NaN; // Assigns Float.NaN to f1 

The expression is of the type double. double to float assignment is not allowed

float f1 = Float.NaN % Double.NaN; // A compile-time error. 
d1 = Float.NaN % Double.NaN; // Assigns Double.NaN to d1 
  

If the right-hand operand is zero, the result is NaN. For example,

  
float f1; 
f1 = 15.0F % 0.0F; // Assigns Float.NaN to f1 

If the left-hand operand is infinity, the result is NaN. For example,

float f1; 
f1 = Float.POSITIVE_INFINITY % 2.1F; // Assigns Float.NaN to f1 

Related Topics