简体   繁体   中英

Difference between % and /

A bit of a simple question I have but this really confuses me as I am a beginner programmer.

What is the difference between % and / in mathematical operations java. If you guys could explain, that will be great!!!

Thanks in advance

The first operator % is called the modulo operator, it will return the remainder on a division, for example:

5 % 3 = 2 ( 3 * 1 = 3 so the remainder to 5 is 2) 4 % 2 = 0 ( 2 * 2 = 4 so the remainder is 0)

The second operator is the integer division as you should already know it, simply divides one number into another, remember, in Java the result of the operations depends on the data type you are using, for example, int doesn't have decimal positions, so 3 / 2 = 1, because you lose the 0.5 decimal part.

% in most programming languages means modulo. / is just regular division.

Modulo is division, but instead of finding how many times a number divides into another number, it finds the remainder.

Example: 15 % 10 = 5

% gives you the reminder of a division.

This operator is used widely to check if a number "a" is a multiple of "b".

Example: 4 % 2 = 0

/ gives you the the quotient of the division. Example: 4 % 2 = 2

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM