当两个值都是double时,为什么没有ArithmeticException(除以零)?

Gop*_*opi 2 java math android exception

看过Double.java的源代码和一些常量就好

/**
 * Constant for the Not-a-Number (NaN) value of the {@code double} type.
 */
public static final double NaN = 0.0 / 0.0;

/**
 * Constant for the positive infinity value of the {@code double} type.
 */
public static final double POSITIVE_INFINITY = 1.0 / 0.0;

/**
 * Constant for the negative infinity value of the {@code double} type.
 */
public static final double NEGATIVE_INFINITY = -1.0 / 0.0;
Run Code Online (Sandbox Code Playgroud)

但我想知道它为什么不抛出ArithmeticException(除以零)?

我试过了

 public static final int VALUE = 0/0;
Run Code Online (Sandbox Code Playgroud)

现在它正在抛出异常,但是当我说

 public static final double VALUE = 0d/0d;
Run Code Online (Sandbox Code Playgroud)

它没有抛出异常......

什么是魔法Double以及它为什么不抛出异常?

Ste*_*n C 11

"神奇"是Java浮点表示基于IEE 754浮点标准.它有一个特殊值(NaN),表示零除以零时得到的"不确定值".(也有代表正负无穷大的值;例如1.0 / 0.0给出INF- 正无穷大.)

这包含在Java语言规范中; 见第§4.2.3其中讨论的陈述和§4.2.4其中讨论如何运算的作品.


需要注意的是相同的"神奇"适用于float,double,FloatDouble.