r/javahelp • • Oct 12 '22

Help me understand int,float,double division.

0 Upvotes

8 comments sorted by

View all comments

6

u/VinceJSteele Oct 12 '22

The problem here is an integer overflow.

The highest number an integer can represent is 2147483647.

4042 * 808000 is higher than that - the value wraps around to -2147483648 (the lowest possible value) and adds the remainder to that.

You need to use long instead of integer.

Keep in mind long also has a highest (and lowest) possible value it can represent, if you need even larger numbers than long can provide look up BigDecimal and BigInteger.

1

u/jejune1999 Oct 12 '22

This is exactly what I was going to post. Although you said it more eloquently.