Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java "Errors" with math - int vs Double [duplicate]

I noticed something weird earlier today. I was writing some code that was supposed to make graphs in complex quadrants. Anyway, I typed int i = 1/0; and it wouldn't compile. When I changed the code to double i = 1.0/0.0; the code compiled fine. When I ran the code it gave an error / by 0. I was expecting that... But why does it compile fine when using doubles and not integers? I am using the Blue J IDE

like image 269
ChriskOlson Avatar asked Jun 28 '26 18:06

ChriskOlson


1 Answers

Dividing an int value by zero would result in a ArithmeticException, hence the expression 1 / 0 is illegal.

The result of dividing a double value by zero is infinity or NaN *, so the expression 1.0 / 0.0 is legal.


*) See t_over's comment for specifics:

like image 190
Tony the Pony Avatar answered Jun 30 '26 07:06

Tony the Pony



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!