I had already searched through different questions on this topic but not get a clear idea.
Check this code:
class Test{
public static void main(String[] s){
int a=5;
float b=(float)a/0;
System.out.print(b);
}
}
the output is Infinity. But the thing I’m not getting is a is an int and a/0 must throw an exception. So how can it show output Infinity?
The binary / operator performs division, producing the quotient of its operands. The left-hand operand is the dividend and the right-hand operand is the divisor.
Integer division rounds toward 0. That is, the quotient produced for operands n and d that are integers after binary numeric promotion (§5.6.2) is an integer value q whose magnitude is as large as possible while satisfying |d·q||n|; moreover, q is positive when |n||d| and n and d have the same sign, but q is negative when |n||d| and n and d have opposite signs. There is one special case that does not satisfy this rule: if the dividend is the negative integer of largest possible magnitude for its type, and the divisor is -1, then integer overflow occurs and the result is equal to the dividend. Despite the overflow, no exception is thrown in this case. On the other hand, 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 specification of IEEE arithmetic:
Despite the fact that overflow, underflow, division by zero, or loss of information may occur, evaluation of a floating-point division operator / never throws a run-time exception.
This can be verified at: http://docs.oracle.com/javase/specs/jls/se5.0/html/expressions.html#15.17.2