Skip to content

What's the syntax for mod in java

The syntax for mod in Java is the percent sign %. For example, a % b returns the remainder of a divided by b.

Here is an example of how you can use the mod operator in a Java program:


java
int a = 5;
int b = 2;
int c = a % b;  // c will be 1

You can also use the mod operator with floating-point numbers. In this case, the result will also be a floating-point number.


java
double a = 5.5;
double b = 2.0;
double c = a % b;  // c will be 1.5

Note that the mod operator has the same precedence as * and /, and higher precedence than + and -. This means that expressions like a + b % c will be evaluated as a + (b % c) rather than (a + b) % c.

Do you find this helpful?

Dual-run preview — compare with live Symfony routes.