Difference between 1/0 and 1.0/0.0 in java

I am new in Java programming language, a stupid question comes up as below.

to execute 1/0 in java will be generated a runtime exception, as the value is undefined.

but the 1.0/0.0 will be okay and the value is infinity.

anything special with double data type? could help have a good explanation for understanding? thanks in advance.

Jon Skeet

anything special with double data type?

Yes, double can represent infinity (as can float), whereas int can't. The floating point types follow IEEE 754, which allows for signed infinity values, as well as "not a number" (NaN) values, such as trying to obtain the square root of -1.

From JLS 15.17.2 (division):

if the value of the divisor in an integer division is 0, then an ArithmeticException is thrown.


The result of a floating-point division is determined by the rules of IEEE 754 arithmetic:

  • ...
  • Division of a zero by a zero results in NaN; division of zero by any other finite value results in a signed zero. The sign is determined by the rule stated above.
  • Division of a nonzero finite value by a zero results in a signed infinity.


See more on this question at Stackoverflow