Is 1/0 a legal Java expression?

前端 未结 8 1224
萌比男神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:41

    It is legal in compiling point of view, but it would throw an exception if executed!

    the reason... well programming must allow flexibility therefore all the expressions and every single code you type is a variable for the compiler, thus in the mathematical expression X/Y the compiler does not care if the Y variable value is (Y==0) or any other number for the compiler this is a variable... if the compiler would have to look at values also, that would be considered runtime, wouldn't it.

提交回复
热议问题