Is 1/0 a legal Java expression?

前端 未结 8 1234
萌比男神i
萌比男神i 2020-11-27 16:47

The following compiles fine in my Eclipse:

final int j = 1/0;
// compiles fine!!!
// throws ArithmeticException: / by zero at run-time

Java

8条回答
  •  孤街浪徒
    2020-11-27 17:31

    Java explicitly requires integer division by zero to trigger an ArithmeticException. The assignment to j can't be elided because that would violate the spec.

提交回复
热议问题