hi,pls wat does this symbol mean(%)?i know it means remainder,but can not seem to understand in what context .

for example if you say

hours = hours%24;
minutes = mins%60;.................

Dani AI

Generated

gave the basic idea; the points below cover Java-specific behaviour and common pitfalls that matter in real code, and tie back to the time-wrapping use mentioned by .

int a = -7 % 3;               // a == -1
int b = Math.floorMod(-7, 3); // b == 2

double c = 5.5 % 2.0;         // c approx 1.5 (floating-point rounding applies)

Important notes and tips:

  • Integer % follows Java's remainder definition; the sign of the result follows the left operand. For a guaranteed non-negative wrap (useful for circular buffers or clock arithmetic), use Math.floorMod or normalize with ((x % n) + n) % n.
  • x % 0 for integer types throws ArithmeticException. For floating-point operands the operation follows IEEE 754 rules and can yield NaN or other special values.
  • % has the same precedence as * and / and is left-associative.
  • For performance where the divisor is a power of two and values are non-negative, x & (n - 1) can replace x % n. Use that only when correctness for negatives is not required.

For the formal definition see the Java Language Specification Remainder operator and the utility method documentation Math.floorMod.

Modulus. It produces the "remainder" from a division. I.E. 10 % 3 returns 1 and 11 % 3 returns 2 and 12 % 3 returns 0.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.