This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.

Dear all as we know when a number divided by 0 the value will be infinity but in java when we divide a number by 0 we will get Exception in thread "main" java.lang.ArithmeticException: / by zero, but when we divide it by 0.0 we will get infinity why it is so like that any how 0.0 ==0 will be true.

When you divide a byte, char, short, int or long by 0, integer division is used, and that does not allow dividing by 0.
When you divide a byte, char, short, int or long by 0.0 (which is a double), double division is used, and that does allow dividing by 0.

With all arithmetic operators in Java, the result has the type of the largest operator. For instance:
- float operator long => float
- int operator int => int
- int operator long => long

However, the result is never byte, char or short; in those cases it will be int. So:
- byte operator short => int

If you divide and integer by 0, you are doing an integer division. This leads to an exception. If you divide a number by 0.0, you are doing a floating-point division, and this results in the special value Infinity.

Note that floating-point numbers have a special value to indicate infinity, while integers do not. Since the value "infinity" cannot be represented by an integer, the only sensible way is to throw an exception when you try to divide by 0.

Since the value "infinity" cannot be represented by an integer, the only sensible way is to throw an exception when you try to divide by 0

Dear all as you told in the above statement that infinity cannot be represented by an integer is there any reason for this..?
Because in mathematics if we divide any number by 0 or 0.0 the value will tends to infinity

It has to do with the format of integral numbers. If you have a capacity of 32 bits, then the most efficient way to fill them is two's complement. It also matches the way computer chips carry out arithmetic. So there is no capacity for an infinite value.

Actually the behaviour of ints is better than that of doubles. The mathematicians do not say that 1 / 0 = infinity. They say 1 / 0 is undefined. So the integer behaviour, throwing an Exception, makes good mathematical sense.

Campbell Ritchie wrote:Actually the behaviour of ints is better than that of doubles. The mathematicians do not say that 1 / 0 = infinity. They say 1 / 0 is undefined. So the integer behaviour, throwing an Exception, makes good mathematical sense.

A while ago I read a book about the history of scientific discoveries, and one of the subjects was how the number 0 was discovered, and what the meaning of dividing by zero was exactly. It took a surprisingly long time before mathematicians worked out what dividing by zero means.