What is modulus operator % in java ?
Answer : % is arithmatic modulus operator. When used it returns the remainder of the division operation. It can be applied to integer types and floating-point types as well. Please see example below of modulus (%) operator:
package com.javahonk.modulustest; public class ModulusTest { public static void main(String[] args) { int x = 26; double y = 26.54; System.out.println("x mod 10 = " + x % 10); System.out.println("y mod 10 = " + y % 10); } }
List of arithmetic operators:
Result | Operator |
Addition | + |
Subtraction (also unary minus) | – |
Multiplication | * |
Division | / |
Modulus | % |
Increment | ++ |
Addition assignment | += |
Subtraction assignment | –= |
Multiplication assignment | *= |
Division assignment | /= |
Modulus assignment | %= |
Decrement | – – |