What does% = mean in Java?

How does% = work in Java? I was told that it can be used to reassign a value?

Grateful if anyone can teach! Thanks!

minutes=0; while(true){ minutes++; minutes%=60; } 
+6
source share
3 answers

This is a shorthand for:

 minutes = minutes % 60; 

There are other similar compound assignment operators for all binary operators in Java: += , -= , *= , ^= , ||= etc.

+15
source

+ = adds to:

 i+=2; 

there is i = i + 2;

% - balance: 126 % 10 is 6.

Expanding this logic, %= set to the rest of:

 minutes%=60; 

sets the minutes to minutes % 60 , that is, the remainder when minutes is divided by 60. This means that the minutes do not overflow for 59.

+2
source

This is aa Modulo operation , which matches the remainder of the division. minutes%=60; matches minutes = minutes % 60; which matches minutes = minutes - (((int) (minutes/60)) * 60);

+1
source

Source: https://habr.com/ru/post/958432/


All Articles