以下陈述java.lang.ArithmeticException: / by zero显而易见.
System.out.println(0/0);
Run Code Online (Sandbox Code Playgroud)
因为文字0被认为是int文字并且在整数算术中不允许除以零.
但是,以下情况不会抛出任何异常java.lang.ArithmeticException: / by zero.
int a = 0;
double b = 6.199;
System.out.println((b/a));
Run Code Online (Sandbox Code Playgroud)
它显示Infinity.
以下语句生成NaN(非数字),没有例外.
System.out.println(0D/0); //or 0.0/0, or 0.0/0.0 or 0/0.0 - floating point arithmetic.
Run Code Online (Sandbox Code Playgroud)
在这种情况下,两个操作数都被认为是双精度数.
同样,以下语句不会抛出任何异常.
double div1 = 0D/0; //or 0D/0D
double div2 = 0/0D; //or 0D/0D
System.out.printf("div1 = %s : div2 = %s%n", div1, div2);
System.out.printf("div1 == div2 : %b%n", div1 == div2);
System.out.printf("div1 …Run Code Online (Sandbox Code Playgroud) double d=1.0/0.0;
Run Code Online (Sandbox Code Playgroud)
输出是 Infinity
double d=1/0;
Run Code Online (Sandbox Code Playgroud)
输出是ArithmeticException.
这两者有什么区别?这里的无限是什么意思?
可能重复:
为什么Java除以0.0时不会抛出异常?
为什么Java中的以下语句不会报告ArithmeticException?
double d = 1.0/0;
Run Code Online (Sandbox Code Playgroud) 看过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)
现在它正在抛出异常,但是当我说 …
我是Java编程语言的新手,下面提出了一个愚蠢的问题.
在java中执行1/0将生成运行时异常,因为该值未定义.
但是1.0/0.0就可以了,价值是无限的.
双数据类型有什么特别之处吗?可以帮助理解一个很好的解释?提前致谢.