The following compiles fine in my Eclipse:
final int j = 1/0;
// compiles fine!!!
// throws ArithmeticException: / by zero at run-time
Java
Well, if you look into the Double class, you will see the following:
/**
* A constant holding the positive infinity of type
* double. It is equal to the value returned by
* Double.longBitsToDouble(0x7ff0000000000000L).
*/
public static final double POSITIVE_INFINITY = 1.0 / 0.0;
The same calculation is made in the Float class, except with floats instead of doubles. Basically, 1/0 returns a really, really big number, larger than Double.MAX_VALUE.
This following code:
public static void main(String[] args) {
System.out.println(Double.POSITIVE_INFINITY);
System.out.println(Double.POSITIVE_INFINITY > Double.MAX_VALUE);
}
Outputs:
Infinity
true
Note the special case in printing out Double.POSITIVE_INFINITY. It prints out a string, though it's regarded as a double.
To answer the question, yes it is legal in Java, but 1/0 resolves to "infinity" and is treated differently from standard Doubles (or floats, or so on and so forth).
I should note that I do not have the slightest clue how or why it was implemented this way. When I see the above output, it all seems like black magic to me.